-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathabcpath.cpp
More file actions
71 lines (57 loc) · 1.48 KB
/
abcpath.cpp
File metadata and controls
71 lines (57 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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 H, W;
char mat[MAXN][MAXN];
int sol_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};
int dfs(int row, int col, char letter) {
if (sol_mat[row][col] != -1) return sol_mat[row][col];
int curr_pos_sol = 1;
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) {
curr_pos_sol = max(curr_pos_sol, 1+dfs(row+dx[i], col+dy[i], letter+1));
}
}
sol_mat[row][col] = curr_pos_sol;
return curr_pos_sol;
}
int main() {
int _case = 0;
while (true) {
scanf("%d%d", &H, &W);
if (!H && !W) return 0;
int sol = 0;
++_case;
memset(sol_mat, -1, sizeof sol_mat);
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;
sol = max(sol, dfs(i, j, 'A'+1));
}
}
printf("Case %d: %d\n", _case, sol);
}
return 0;
}