-
Notifications
You must be signed in to change notification settings - Fork 2
/
ABCPATH - ABC Path.cpp
72 lines (63 loc) · 1.34 KB
/
ABCPATH - ABC Path.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
//ABCPATH - ABC Path
#include<bits/stdc++.h>
using namespace std;
#define ll long long
char arr[55][55];
ll H,W;
ll dp[55][55];
vector <pair<ll,ll> > start;
ll fun(ll x,ll y)
{
if(dp[x][y]!=-1)
return dp[x][y];
ll temp=0;
if(arr[x-1][y]==arr[x][y]+1)
temp=max(temp,1+fun(x-1,y));
if(arr[x+1][y]==arr[x][y]+1)
temp=max(temp,1+fun(x+1,y));
if(arr[x][y+1]==arr[x][y]+1)
temp=max(temp,1+fun(x,y+1));
if(arr[x][y-1]==arr[x][y]+1)
temp=max(temp,1+fun(x,y-1));
if(arr[x+1][y+1]==arr[x][y]+1)
temp=max(temp,1+fun(x+1,y+1));
if(arr[x+1][y-1]==arr[x][y]+1)
temp=max(temp,1+fun(x+1,y-1));
if(arr[x-1][y-1]==arr[x][y]+1)
temp=max(temp,1+fun(x-1,y-1));
if(arr[x-1][y+1]==arr[x][y]+1)
temp=max(temp,1+fun(x-1,y+1));
return dp[x][y]=temp;
}
int main()
{
ll test=0;
while(1)
{
scanf("%lld%lld",&H,&W);
if(H==0)
break;
memset(dp,-1,sizeof dp);
memset(arr,0,sizeof arr);
start.clear();
ll i,j;
for(i=1;i<=H;i++)
{
string str;
cin>>str;
for(j=1;j<=W;j++)
{
arr[i][j]=str[j-1];
if(str[j-1]=='A')
start.push_back(make_pair(i,j));
}
}
// cout<<start.size()<<"<--statr"<<endl;
ll ans=0;
for(i=0;i<start.size();i++)
ans=max(ans,1+fun(start[i].first,start[i].second));
test++;
printf("Case %lld: %lld\n",test,ans);
}
return 0;
}