标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 27072 | Accepted: 13392 |
Description
Input
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
Source
1 //num个棋盘空位,k个棋子,其实就是八皇后问题的变形。空位一个一个去填补,但要注意顺序。 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <queue> 6 #include <stack> 7 #include <iostream> 8 using namespace std; 9 char map[10][10]; 10 bool col[10],row[10]; 11 struct node{ 12 int x,y; 13 }; 14 node m[65]; 15 int sum,k,n,num; 16 void dfs(int num1){ 17 int x=m[num1].x; 18 int y=m[num1].y; 19 if(row[x]||col[y]){ 20 return; 21 } 22 row[x]=true;//回溯 23 col[y]=true; 24 k--; 25 if(!k){ 26 sum++; 27 } 28 else{ 29 int i; 30 for(i=num1+1;i<num;i++){ 31 dfs(i); 32 } 33 } 34 k++; 35 row[x]=false; 36 col[y]=false; 37 } 38 int main(){ 39 //freopen("D:\\INPUT.txt","r",stdin); 40 while(scanf("%d %d",&n,&k)!=EOF){ 41 if(n==-1&&k==-1){ 42 break; 43 } 44 memset(col,false,sizeof(col)); 45 memset(row,false,sizeof(row)); 46 int i,j; 47 num=0; 48 sum=0; 49 for(i=0;i<n;i++){ 50 for(j=0;j<n;j++){ 51 cin>>map[i][j]; 52 if(map[i][j]==‘#‘){ 53 m[num].x=i; 54 m[num++].y=j; 55 } 56 } 57 } 58 for(i=0;num-i>=k;i++){ 59 dfs(i); 60 } 61 //cout<<sum<<endl; 62 cout<<sum<<endl; 63 } 64 return 0; 65 }
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/4646009.html