| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 21669 | Accepted: 10768 |
Description
Input
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
搜索吧.......!!!
一行一行的找下去,也可以在某些不填棋子。
AC代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int n,m,tot;
char map[10][10];
int x[10],y[10];
void dfs(int h,int ans)
{
int i;
if(ans==m)
tot++;
else
{
for(i=0;i<n;i++)//在h行填棋。
{
if(map[h][i]=='#'&&!x[h]&&!y[i]&&h<n)
{
x[h]=1;y[i]=1;
dfs(h+1,ans+1);
x[h]=0;y[i]=0;
}
}
if(h+1<n)
dfs(h+1,ans);//跳过h行不填棋。
}
}
int main()
{
int i,j;
while(cin>>n>>m,n!=-1&&m!=-1)
{
memset(x,0,sizeof x);
memset(y,0,sizeof y);
tot=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>map[i][j];
dfs(0,0);//从0行开始找,起始填棋子0个。
cout<<tot<<endl;
}
return 0;
}
原文地址:http://blog.csdn.net/hanhai768/article/details/37362063