pku 1321 棋盘问题 DFS

http://poj.org/problem?id=1321

自己真是本了开始写了歌对于棋子有顺序的搜索,导致求出来是ans的阶乘种数了,因为k < n且不能在同一行同一列,只要按行搜索,记录列是否被访问过即可!

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 12
using namespace std;

char map[maxn][maxn];
bool vt[maxn];
int n,m,ans;


void dfs(int x,int len)
{
    int i,j;
    if (len == m)
    {
        ans++;
        return ;
    }
    for (i = x; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            if (!vt[j] && map[i][j] == '#')
            {
                vt[j] = true;
                dfs(i + 1,len + 1);
                vt[j] = false;
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int i;
    while (scanf("%d%d",&n,&m))
    {
        if (n == -1 && m == -1) break;
        for (i = 0; i < n; ++i) scanf("%s",map[i]);
        memset(vt,false,sizeof(vt));
        ans = 0;
        dfs(0,0);
        printf("%d\n",ans);
    }
    return 0;
}
posted @ 2012-04-13 10:42  E_star  阅读(217)  评论(0编辑  收藏  举报