标签:
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<map> #include<list> #include<set> #include<stack> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; char mp[5][5]; int n, res; bool vis[5][5]; void print(){ for(int i=0; i<n; i++) { for(int j=0;j <n; j++){ cout<<mp[i][j]<<" "; }cout<<endl; } cout<<endl; } bool check(int x, int y) { int dir[4][2] = {0,1, 1,0, -1,0, 0,-1}; bool f[4] = {1, 1, 1, 1}; for(int d=1; d<n; d++){ for(int i=0; i<4; i++){ int tx = x+d*dir[i][0]; int ty = y+d*dir[i][1]; if(tx < 0 || ty<0 || tx>=n && ty>=n) continue; if(!f[i]) continue; if(mp[tx][ty] == ‘X‘) f[i] = false; else if(mp[tx][ty] == ‘1‘) return false; } } return true; } int dfs(int x, int y,int cnt) { for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(mp[i][j] == ‘.‘ && check(i, j)){ res = max(res, cnt); mp[i][j] = ‘1‘; vis[i][j] = true; // print(); dfs(i, j, cnt+1); vis[i][j] = false; mp[i][j] = ‘.‘; } } } return res; } int main() { while(scanf("%d", &n) != EOF && n) { memset(vis, false, sizeof(vis)); for(int i=0; i<n; i++){ scanf("%s", mp[i]); } res = 0; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(mp[i][j] == ‘.‘){ mp[i][j] == ‘1‘; int temp = dfs(i, j, 1); res = max(res, temp); } } } printf("%d\n", res); } return 0; }
标签:
原文地址:http://www.cnblogs.com/vegg117/p/4319360.html