-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPOJ-ABCPATH.cpp
More file actions
61 lines (50 loc) · 1.28 KB
/
SPOJ-ABCPATH.cpp
File metadata and controls
61 lines (50 loc) · 1.28 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
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int N = 52;
int h, w;
int dp[N][N];
char grid[N][N];
int dx[] = { 0, 0, 1, 1, 1, -1, -1, -1 };
int dy[] = { 1, -1, 0, 1, -1, 0, 1, -1 };
int solve(int x, int y) {
int &ret = dp[x][y];
if (~ret) return ret;
ret = 0;
for (int i = 0; i < 8; ++i) {
int px = x + dx[i];
int py = y + dy[i];
if (px >= 0 && py >= 0 && px < h && py < w && grid[px][py] == grid[x][y] + 1) {
ret = max(ret, solve(px, py) + 1);
}
}
return ret;
}
int main(){
ios::sync_with_stdio(0); cin.tie(nullptr);
int caso = 1;
while (cin >> h >> w && (h || w)) {
memset(dp, -1, sizeof dp);
cout << "Case " << caso++ << ": ";
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> grid[i][j];
}
}
int ans = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (grid[i][j] == 'A') {
ans = max(ans, solve(i, j) + 1);
}
}
}
cout << ans << endl;
}
return 0;
}