forked from Anmol2307/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABCPATH.cpp
105 lines (90 loc) · 2.05 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Author: Hakobyan Tigran
*/
#pragma comment(linker, "/STACK:60777216")
#define printTime(begin, end) printf("%.3lf\n", (double)(end - begin) / (double)CLOCKS_PER_SEC)
#include <string.h>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <functional>
#include <complex>
#include <iostream>
#include <fstream>
#include <sstream>
#include <bitset>
#include <limits>
#include <ctime>
#include <cassert>
#include <valarray>
using namespace std;
#define IN(a) freopen(a, "r", stdin)
#define OUT(a) freopen(a, "w", stdout)
#define mp(a, b) make_pair(a, b)
#define det(a, b, c, d) (a * d - c * b)
#define sbstr(s, i, j) s.substr(i, j - i + 1)
#define clear(dp) memset(dp, -1, sizeof(dp))
#define reset(arr) memset(arr, 0, sizeof(arr))
#define EPS 1e-9
#define PI acos(-1.0)
#define MOD 1000000007
#define IINF 1000000000
#define LINF 6000000000000000000LL
int n, m;
char a[55][55];
int d[55][55];
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
bool valid (int x, int y) {
return x >= 0 && y >= 0 && x < n && y < m;
}
int dfs (int x, int y) {
if(d[x][y] != -1)
return d[x][y];
d[x][y] = 0;
for(int k = 0; k < 8; ++k) {
int n_x = x + dx[k];
int n_y = y + dy[k];
if(valid(n_x, n_y)) {
if(a[n_x][n_y] == a[x][y] + 1) {
d[x][y] = max(d[x][y], 1 + dfs(n_x, n_y));
}
}
}
return d[x][y];
}
int main () {
int test_id(0);
while(scanf("%d%d", &n, &m)) {
if(n == 0 && m == 0)
break;
for(int i = 0; i < n; ++i) {
scanf("%s", a[i]);
}
int ans = 0;
for(int i = 0; i < 55; ++i) {
for(int j = 0; j < 55; ++j) {
d[i][j] = -1;
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(a[i][j] == 'A' && d[i][j] == -1) {
ans = max(ans, dfs(i, j));
}
}
}
printf("Case %d: %d\n", ++test_id, ans + 1);
}
return 0;
}