题意:
n×m的格子,每个格子有一个大写英文字母,每个格子可以沿着八个方向走。问图中最长的从A开始的字母序列(ABCD...)的长度是多少。
搜索...
注意写vis标记,否则容易炸。
可以试试这组数据:
50 50 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA ABCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCBA ABCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDCBA ABCDEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDCBA ABCDEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEDCBA ABCDEFGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGFEDCBA ABCDEFGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHGFEDCBA ABCDEFGHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIHGFEDCBA ABCDEFGHIJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJIHGFEDCBA ABCDEFGHIJKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKJIHGFEDCBA ABCDEFGHIJKLLLLLLLLLLLLLLLLLLLLLLLLLLLLKJIHGFEDCBA ABCDEFGHIJKLMMMMMMMMMMMMMMMMMMMMMMMMMMLKJIHGFEDCBA ABCDEFGHIJKLMNNNNNNNNNNNNNNNNNNNNNNNNMLKJIHGFEDCBA ABCDEFGHIJKLMNOOOOOOOOOOOOOOOOOOOOOONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPPPPPPPPPPPPPPPPPPPPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQQQQQQQQQQQQQQQQQQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRRRRRRRRRRRRRRRRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSSSSS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KKKKKKKKKKKKKKKKKKKKKKKKKJIHGFEDCBA ABCDEFGHIJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJIHGFEDCBA ABCDEFGHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIHGFEDCBA ABCDEFGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHGFEDCBA ABCDEFGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGFEDCBA ABCDEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEDCBA ABCDEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDCBA ABCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDCBA ABCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCBA ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
#include <cstdio> #include <algorithm> using namespace std; const int maxn = 55, maxq = 10000; int n, m, g[maxn][maxn], vis[maxn][maxn]; int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dy[] = {0, -1, -1, -1, 0, 1, 1, 1}; struct _point { int x, y; } q[maxq]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } int ans, clo; inline void bfs(int sx, int sy) { int h = 0, t = 0; q[t++] = (_point) {sx, sy}; vis[sx][sy] = clo; while(h != t) { _point u = q[h++]; ans = max(ans, g[u.x][u.y]); for(int i = 0; i < 8; i++) { int x = u.x + dx[i], y = u.y + dy[i]; if(x >= 1 && x <= n && y >= 1 && y <= m) if((g[x][y] == g[u.x][u.y] + 1) && (vis[x][y] != clo)) vis[x][y] = clo, q[t++] = (_point) {x, y}; } } } int main() { int cas = 0; while(1) { n = iread(); m = iread(); if(n == 0 && m == 0) break; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { char ch = getchar(); for(; ch < 'A' || ch > 'Z'; ch = getchar()); g[i][j] = ch - 'A' + 1; vis[i][j] = 0; } ans = clo = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(g[i][j] == 1) clo++, bfs(i, j); printf("Case %d: %d\n", ++cas, ans); } return 0; }