标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10297 Accepted Submission(s):
4634
//题目卡了dfs,所以TLE了。。 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int map[10][10],cnt,n; void puts(int x,int y) { for(int i=0;i<n;i++){ map[i][y]++; map[x][i]++; if(x+y-i<n&&x+y-i>=0) map[i][x+y-i]++; if(y-x+i<n&&y-x+i>=0) map[i][y-x+i]++; } } void remove(int x,int y) { for(int i=0;i<n;i++){ map[i][y]--; map[x][i]--; if(x+y-i<n&&x+y-i>=0) map[i][x+y-i]--; if(y-x+i<n&&y-x+i>=0) map[i][y-x+i]--; } } void search(int cur) { if(cur==n) cnt++; else{ for(int i=0;i<n;i++){ if(map[i][cur]) continue; puts(i,cur); search(cur+1); remove(i,cur); } } } int main() { while(cin>>n,n){ cnt=0; memset(map,0,sizeof(map)); search(0); cout<<cnt<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4334059.html