-
Notifications
You must be signed in to change notification settings - Fork 1
/
ABCPATH.cpp
84 lines (74 loc) · 2.02 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
#include <bits/stdc++.h>
#include <cstdio>
#define ll long
#define get(a) scanf("%ld", &a);
#define rep(n) for( ll i = 0; i < n; i++ )
#define repVector(v) for(vector<ll>::iterator it = v.begin(); it != v.end(); it++ )
#define repSet(s) for( set<ll>::iterator it = s.begin(); it != s.end(); it++ )
#define all(c) c.begin(), c.end()
#define repu(a,n) for( ll i = a; i <= n; i++ )
#define pb push_back
#define min(a,b) ( (a < b) ? a : b )
#define max(a,b) ( (a > b) ? a : b )
#define absol(a) ( ( a >= 0 ) ? a : -a )
#define FOR(i,a,b) for( ll i = a; i < b; i++ )
#define matrix vector< vector<ll> >
#define MOD 1000000007
#define F first
#define S second
#define L 2*r
#define R 2*r+1
#define mp make_pair
using namespace std;
ll ans = 0;
ll h, w;
string str[55];
ll dp[55][55];
bool valid( ll i, ll j ) {
if( i < 0 || j < 0 )
return 0;
if( i >= h || j >= w )
return 0;
return 1;
}
int rec( ll i, ll j , char A = 'A', ll c = 1 ) {
ans = max( ans, c );
ll val = c;
if( dp[i][j] )
return dp[i][j];
if( valid(i-1,j-1) && str[i-1][j-1] == A+1 )
val = max( val, rec(i-1, j-1, A+1, c+1 ) );
if( valid(i-1,j) && str[i-1][j] == A+1 )
val = max( val, rec(i-1, j, A+1, c+1 ) );
if( valid(i+1, j+1) && str[i+1][j+1] == A+1 )
val = max( val, rec(i+1, j+1, A+1, c+1 ) );
if( valid(i+1,j) && str[i+1][j] == A+1 )
val = max( val, rec(i+1, j, A+1, c+1 ) );
if( valid(i,j+1) && str[i][j+1] == A+1 )
val = max( val, rec(i, j+1, A+1, c+1 ) );
if( valid( i, j-1 ) && str[i][j-1] == A+1 )
val = max( val, rec(i, j-1, A+1, c+1 ) );
if( valid( i-1, j+1) && str[i-1][j+1] == A+1 )
val = max( val, rec(i-1, j+1, A+1, c+1 ) );
if( valid( i+1, j-1 ) && str[i+1][j-1] == A+1 )
val = max( val, rec(i+1, j-1, A+1, c+1 ) );
dp[i][j] = val;
return c;
}
int main() {
ll t = 0;
while(1) {
ans = 0;
get(h); get(w);
if( h == 0 && w == 0 )
return 0;
memset( dp, 0, sizeof dp );
cin.ignore();
rep(h) { cin >> str[i]; }
FOR(i,0,h)
FOR(j,0,w)
if( str[i][j] == 'A' )
rec( i,j );
printf("Case %ld: %ld\n", ++t, ans);
}
}