这么简单一道题目,想了这么久,还wa了一次,不可饶恕啊,越来越觉得自己笨了,dfs搜索都快不会写了。。。逻
辑混乱,一定得认真想题目,认真写代码:
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
#define MAX 0x7fffffff
using namespace std;
int sum,cnt,m,ans;
struct node
{
int i,j;
}gra[70];
int visit[2][70];
void dfs(int num,int ans)
{
if(ans == m)
{
cnt++;
return ;
}
for(int i=num+1; i<=sum; i++)
{
if(!visit[0][gra[i].i] && !visit[1][gra[i].j])
{
visit[0][gra[i].i] = 1;
visit[1][gra[i].j] = 1;
ans ++;
dfs(i,ans);
ans --;
visit[1][gra[i].j] = 0;
visit[0][gra[i].i] = 0;
}
}
return ;
}
int main()
{
int n,k,i,j;
char ch;
while(cin >> n >> m)
{
if(n == -1 && m == -1)
break;
memset(visit,0,sizeof(visit));
cnt = 0;
getchar();
int k = 1;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
cin >> ch;
if(ch == '#')
{
gra[k].i = i;
gra[k].j = j;
k++;
}
}
getchar();
}
sum = k-1;
dfs(0,0);
cout << cnt << endl;
}
return 0;
}