标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12279 Accepted Submission(s): 5535
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 int n,cnt; 6 int vis[11];//标记数组 7 int picture[11];//用下标来表示行号,自身存储列号。 8 bool judge(int r,int r2,int c2)//判断2个皇后是否在棋盘边框成45角的斜线上 9 { 10 for(int i=0;i<=r;i++)//遍历已经放置皇后的点 11 { 12 if(i+picture[i]==r2+c2||i-picture[i]==r2-c2) 13 return false; 14 } 15 return true; 16 } 17 void dfs(int row,int column,int num) 18 { 19 if(num==n) 20 { 21 cnt++; 22 return; 23 } 24 for(int i=0;i<n;i++) 25 if(!vis[i]&&judge(row,row+1,i)) 26 { 27 picture[row+1]=i; 28 //printf("%dorz%dorz%d\n",row+1,i,num); 29 vis[i]=1; 30 dfs(row+1,i,num+1); 31 vis[i]=0; 32 } 33 } 34 int main() 35 { 36 while(~scanf("%d",&n)) 37 { 38 cnt=0; 39 memset(vis,0,sizeof(vis)); 40 memset(picture,0,sizeof(picture)); 41 for(int i=0;i<n;i++) 42 { 43 vis[i]=1; 44 picture[0]=i; 45 dfs(0,i,1); 46 vis[i]=0;//回溯法:还原标记 47 } 48 printf("%d\n",cnt); 49 } 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/ZP-Better/p/4649657.html