639--Don't Get Rooked

///简单回溯 #include<stdio.h> #include<string.h> #define LOCAL char s[7][7]; int flag[7][7]; int n,max,num,total,k; int test(int x,int y) { int i,j,ok=1; j=y; for(i=x-1;i>=0;i--) { if(flag[i][j]==1) { ok=0; break; } if(flag[i][j]==-1) break; } for(i=x+1;i<n;i++) { if(flag[i][j]==1) { ok=0; break; } if(flag[i][j]==-1) break; } i=x; for(j=y-1;j>=0;j--) { if(flag[i][j]==1) { ok=0; break; } if(flag[i][j]==-1) break; } for(j=y+1;j<n;j++) { if(flag[i][j]==1) { ok=0; break; } if(flag[i][j]==-1) break; } return ok; } void solve() { int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) if(flag[i][j]==0) if(test(i,j)) { num++; if(num>max) max=num; flag[i][j]=1; solve(); flag[i][j]=0; num--; } } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif while(1) { scanf("%d",&n); if(!n) break; int i,j; max=0;num=0;total=0;k=0; for(i=0;i<n;i++) scanf("%s",s[i]); for(i=0;i<n;i++) for(j=0;j<n;j++) if(s[i][j]=='.') { flag[i][j]=0; total++; } else flag[i][j]=-1; solve(); printf("%d/n",max); } return 0; }

你可能感兴趣的:(ini)