转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=1321
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
Description
Input
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
#include <cstring>
#define TM 17
char chess[TM][TM];
int col[TM];
int n, k, ans;
void dfs(int row, int num)
{
if(num == k)
{
ans++;
return;
}
if(row > n)
return;
for(int j = 1; j <= n; j++)
{
if(chess[row][j]!='.'&&!col[j])
{
col[j] = 1;
dfs(row+1,num+1);
col[j] = 0;
}
}
dfs(row+1,num);//如果上一行不能放,就row+1搜索下一行
return;
}
int main()
{
while(cin>>n>>k)
{
if(n == -1 && k == -1)
break;
ans = 0;
memset(col,0,sizeof(col));
for(int i = 1; i <= n; i++)
{
cin>>chess[i]+1;
}
dfs(1,0);
cout<<ans<<endl;
}
return 0;
}poj1321 棋盘问题(深搜dfs),布布扣,bubuko.com
原文:http://blog.csdn.net/u012860063/article/details/37831485