ACM篇 : POJ 1321 -- 棋盤問題

審題錯誤,可放和不可放的符號搞反了,浪費了好多時間。

深搜。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 8;
bool board[MAX+1][MAX+1];
bool occu[MAX+1];
int n;
int k;
int ans;
int readchar()
{
    int t;
    while (t = getchar())
    {
        if (t == '.' || t == '#') 
            break;

    }
    return t;
}

void _init()
{
    memset(board, 0, sizeof(board));
    memset(occu, 0, sizeof(occu));
}
void read_board()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            board[i][j] = (readchar() == '#')  ? true : false;
}

void _dfs(int row, int laid)
{
    if (laid == k)
    {
        ans++;
        return;
    }
    if (row > n) 
        return;

    for (int j = 1; j <= n; j++)
    {
        if (board[row][j] && !occu[j])
        {
            occu[j] = true;
            _dfs(row+1, laid+1);
            occu[j] = false;
        }
    }
    _dfs(row+1, laid);
}
int main()
{
    while (scanf("%d%d", &n, &k) && n != -1 && k != -1)
    {
        _init();
        read_board();

        ans = 0;
        _dfs(1, 0);

        printf("%d\n", ans);
    }
    return 0;
}
發佈了58 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章