题目链接:1388 六边形平面
思路:乍一看,这不是挺简单的吗?判断有没有三个紧贴在一起的,或者两个贴在一起的。然后信心满满敲完了,交了,WA了。还好51nod可以下载数据,一看数据,哇!原来还有环!!然后再dfs跑过去看看有没有奇数环就好了。终于AC了!
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 50 + 5; 5 char s[maxn][maxn]; 6 int n; 7 int dx[] = {0, 0, 1, 1}; 8 int dy[] = {1, 0, 0, -1}; 9 bool valid(int x, int y) { 10 return x>=0&&y>=0&&x<n&&y<n; 11 } 12 int ety(int x, int y) { 13 return x*n+y; 14 } 15 int vis[maxn*maxn]; 16 bool OK; 17 int tx[] = {0, -1, -1, 0, 1, 1}; 18 int ty[] = {1, 1, 0, -1, -1, 0}; 19 void dfs(int v, int u, int deep) { 20 if(vis[v]) { 21 if((deep - vis[v])&1) OK = true; 22 return ; 23 } 24 bool repeat = false; 25 vis[v] = deep; 26 int x = v / n, y = v % n; 27 for(int i=0; i<6; i++) { 28 if(valid(x+tx[i], y+ty[i]) == false || s[x+tx[i]][y+ty[i]] != 'X') continue; 29 if(repeat == false && ety(x+tx[i], y+ty[i]) == u) { 30 repeat = true; continue; 31 } 32 dfs(ety(x+tx[i], y+ty[i]), v, deep+1); 33 } 34 } 35 int main() { 36 freopen("in.txt", "r", stdin); 37 freopen("out.txt", "w", stdout); 38 int T; 39 scanf("%d", &T); 40 while(T--) { 41 memset(vis, 0, sizeof(vis)); 42 scanf("%d", &n); 43 getchar(); 44 int ans = 0; 45 for(int i=0; i<n; i++) { 46 gets(s[i]); 47 } 48 OK = false; 49 for(int i=0; i<n; i++) { 50 for(int j=0; j<n; j++) { 51 if(vis[ety(i, j)] == 0 && s[i][j] == 'X') dfs(ety(i, j), -1, 1); 52 int t = 0; 53 for(int k=0; k<3; k++) { 54 if(valid(i+dx[k], j+dy[k]) && s[i+dx[k]][j+dy[k]] == 'X') t ++; 55 } 56 ans = max(ans, t); 57 t = 0; 58 for(int k=1; k<4; k++) { 59 if(valid(i+dx[k], j+dy[k]) && s[i+dx[k]][j+dy[k]] == 'X') t ++; 60 } 61 ans = max(ans, t); 62 } 63 } 64 if(ans != 2) printf("%d ", ans); 65 else { 66 printf("%d ", ans+OK); 67 } 68 } 69 return 0; 70 }