问题描述:
Description
Input
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
#include"cstdio" #include"queue" using namespace std; const int MAXN=20; typedef pair<int,int> P; P vec[MAXN]; int cnt; char map[MAXN][MAXN]; int n,k; int visr[MAXN]; int visc[MAXN]; int ans; void dfs(int i,int j) { if(j==k) { ans++; return ; } if(i==cnt) return ; P no=vec[i]; if(!visr[no.first]&&!visc[no.second]) { visr[no.first]=1; visc[no.second]=1; dfs(i+1,j+1); visr[no.first]=0; visc[no.second]=0; dfs(i+1,j); } else dfs(i+1,j); } int main() { while(scanf("%d%d",&n,&k)!=EOF&&n!=-1&k!=-1) { cnt=0; scanf("%*c"); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%c",&map[i][j]); if(map[i][j]==‘#‘) { vec[cnt++]=P(i,j); } } scanf("%*c"); } ans=0; dfs(0,0); printf("%d\n",ans); } return 0; }
原文:http://www.cnblogs.com/program-ccc/p/5001700.html