标签:
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
5 1 5 2 4
#include <iostream> #include<algorithm> #include<cstdio> using namespace std; char mp[5][5]; int i,n,ans; int check(int x,int y) { for(int i=x-1;i>=0;i--) { if (mp[i][y]==‘p‘) return 0; if (mp[i][y]==‘X‘) break; } for(int j=y-1;j>=0;j--) { if (mp[x][j]==‘p‘) return 0; if (mp[x][j]==‘X‘) break; } return 1; } void dfs(int k,int num) { int x=k/n,y=k%n; if (k==n*n) { ans=max(num,ans); return; } if (mp[x][y]==‘.‘) if (check(x,y)) { mp[x][y]=‘p‘; dfs(k+1,num+1); mp[x][y]=‘.‘; } dfs(k+1,num); return; } int main() { while(scanf("%d",&n),n) { for(i=0;i<n;i++) scanf("%s",&mp[i]); ans=0; dfs(0,0); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5648408.html