forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
abcpath.cpp
36 lines (36 loc) · 1.18 KB
/
abcpath.cpp
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
// 2014-05-01
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int main() {
for (int cs = 1;; cs++) {
int R, C; cin >> R >> C; if (R==0) return 0;
vector<string> V(R);
vector<vector<int> > dp(R, vector<int>(C, -1e9));
for (int i = 0; i < R; i++) {
cin >> V[i];
}
int res = 0;
for (char c = 'A'; c <= 'Z'; c++) {
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (V[i][j] != c) continue;
if (c == 'A') dp[i][j] = 1;
for (int k = 0; k < 8; k++) {
int r = i + dx[k], c = j + dy[k];
if (r >= 0 && c >= 0 && r < R && c < C &&
V[r][c] == V[i][j] - 1) {
dp[i][j] = max(dp[i][j], dp[r][c] + 1);
}
}
res = max(res, dp[i][j]);
}
}
}
cout << "Case " << cs << ": " << res << endl;
}
}