-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABCPATH.cpp
105 lines (104 loc) · 3.03 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
#include <bits/stdc++.h>
using namespace std;
char a[60][60];
int maxi=0,h,w;
int visited [60][60]={0};
int dfs(int i,int j,int asc)
{
int g= (int) a[i][j];
//maxi=max (maxi,g-64);
if (asc==90)
{
visited[i][j]=26;
return 26;
}
char ch=(char)asc+1;
//cout<<ch<<endl;
//if (j+1<w && visited[i][j+1]==0 && a[i][j+1]==ch)
//cout<<"**"<<endl;
int wo,x,y,z,c,d,e,f;
wo=x=y=z=c=d=e=f=g-64;
if (i+1<h && a[i+1][j]==ch && visited[i+1][j])
wo=visited[i+1][j];
if (j+1<w && a[i][j+1]==ch && visited[i][j+1])
x=visited[i][j+1];
if (i-1>=0 && a[i-1][j]==ch && visited[i-1][j])
y=visited[i-1][j];
if (i+1<h && j+1<w && a[i+1][j+1]==ch && visited[i+1][j+1])
e=visited[i+1][j+1];
if (j-1>=0 && a[i][j-1]==ch && visited[i][j-1])
z=visited[i][j-1];
if (i-1>=0 && j-1>=0 && a[i-1][j-1]==ch && visited[i-1][j-1])
c=visited[i-1][j-1];
if (i+1<h && j-1>=0 && a[i+1][j-1]==ch && visited[i+1][j-1])
d=visited[i+1][j-1];
if (i-1>=0 && j+1<w && visited[i-1][j+1])
f=visited[i-1][j+1];
if (i+1<h && a[i+1][j]==ch && !visited[i+1][j])
wo=dfs (i+1,j,asc+1);
//cout<<"&&"<<j<<" "<<w<<endl;
if (j+1<w && a[i][j+1]==ch && visited[i][j+1]==0)
//cout<<"##"<<endl;
if (j+1<w && a[i][j+1]==ch && !visited[i][j+1])
{ //cout<<"^^"<<endl;
x=dfs (i,j+1,asc+1);
//cout<<i<<" "<<j<<endl;
//cout<<x<<endl;
}
if (i-1>=0 && a[i-1][j]==ch && !visited[i-1][j])
y=dfs (i-1,j,asc+1);
if (j-1>=0 && a[i][j-1]==ch && !visited[i][j-1])
z=dfs (i,j-1,asc+1);
if (i-1>=0 && j-1>=0 && a[i-1][j-1]==ch && !visited[i-1][j-1])
c=dfs (i-1,j-1,asc+1);
if (i+1<h && j-1>=0 && a[i+1][j-1]==ch && !visited[i+1][j-1])
d=dfs (i+1,j-1,asc+1);
if (i+1<h && j+1<w && a[i+1][j+1]==ch && !visited[i+1][j+1])
e=dfs (i+1,j+1,asc+1);
if (i-1>=0 && j+1<w && a[i-1][j+1]==ch && !visited[i-1][j+1])
f=dfs (i-1,j+1,asc+1);
c=max(c,d);
e=max(e,f);
wo=max(wo,x);
y=max(y,z);
c=max(c,e);
wo=max(wo,y);
c=max(c,wo);
visited[i][j]=c;
return c;
}
int main()
{
int i,j,k,t;
int e=1;
while (1)
{
cin>>h>>w;
for (i=0;i<h;i++)
for (j=0;j<w;j++)
a[i][j]=1;
if (!h && !w)
break;
for (i=0;i<h;i++)
for (j=0;j<w;j++)
visited[i][j]=0;
for (i=0;i<h;i++)
for (j=0;j<w;j++)
while (a[i][j]<65 || a[i][j]>90)
cin>>a[i][j];
maxi=0;
for (i=0;i<h;i++)
for (j=0;j<w;j++)
if (a[i][j]=='A' && !visited[i][j])
dfs(i,j,65);
for (i=0;i<h;i++)
{for (j=0;j<w;j++)
//cout<<visited[i][j]<<" ";
// cout<<endl;
maxi=max(visited[i][j],maxi);
}
cout<<"Case "<<e<<": "<<maxi<<endl;
e++;
}
return 0;
}