Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cctype>
- #include <climits>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <sstream>
- #include <stack>
- #include <string>
- #include <vector>
- #define inf 1000000000
- #define MAXN 51
- using namespace std;
- int sol = 0;
- int H, W;
- char mat[MAXN][MAXN];
- int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
- int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
- void dfs(int row, int col, int tmp_sol, char letter) {
- if (tmp_sol > sol) sol = tmp_sol;
- for (int i = 0; i < 8; ++i) {
- if (row+dx[i] < 0 || row+dx[i] >= H || col+dy[i] < 0 || col+dy[i] >= W) continue;
- if (mat[row+dx[i]][col+dy[i]] == letter) {
- dfs(row+dx[i], col+dy[i], tmp_sol+1, letter+1);
- }
- }
- }
- int main() {
- int _case = 1;
- while (true) {
- scanf("%d%d", &H, &W);
- if (!H && !W) return 0;
- sol = 0;
- for (int i = 0; i < H; ++i) {
- scanf("%s", mat[i]);
- }
- for (int i = 0; i < H; ++i) {
- for (int j = 0; j < W; ++j) {
- if (mat[i][j] != 'A') continue;
- dfs(i, j, 1, 'A'+1);
- }
- }
- printf("Case %d: %d\n", _case, sol);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement