-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabcpath.cpp
68 lines (46 loc) · 863 Bytes
/
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
#include<iostream>
using namespace std;
char graph[52][52];
int mark[52][52];
int res=0;
int h,w;
int p[]={-1,-1,-1,0,0,1,1,1};
int q[]={-1,0,1,-1,1,-1,0,1};
int max(int i,int j) {
return (i>j) ? i:j ;
}
void dfs(int x,int y,int cnt) {
//res=max(res,cnt);
if(cnt>res) res=cnt;
for(int i=0;i<8;++i) {
if(x+p[i]>=0 && x+p[i]<h && y+q[i]>=0 && y+q[i]<w && mark[x+p[i]][y+q[i]]==0) {
if(graph[x+p[i]][y+q[i]]==graph[x][y]+1){
mark[x+p[i]][y+q[i]]=1;
dfs(x+p[i],y+q[i],cnt+1);
}
}
}
}
int main() {
int t=0;
while(cin>>h && h>0 && cin>>w && w>0) {
res=0;
for(int i=0;i<h;++i)
{
for(int j=0;j<w;++j) {
cin>>graph[i][j];
mark[i][j]=0;
}
}
for(int i=0;i<h;++i) {
for(int j=0;j<w;++j) {
if(graph[i][j]=='A') {
mark[i][j]=1;
dfs(i,j,1);
}
}
}
cout<<"Case "<<++t<<": "<<res<<endl;
}
return 0;
}