-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabcpath.cpp
More file actions
105 lines (92 loc) · 1.99 KB
/
abcpath.cpp
File metadata and controls
105 lines (92 loc) · 1.99 KB
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;
#define MOD 1000000007
#define pr pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
int A[51][51],dist[51][51],H,W;
bool in_range(int x, int y) {
if(x < H && y < W&& x >= 0 && y >= 0)
return true;
return false;
}
void dfs(int i,int j,int d) {
int x, y;
dist[i][j] = d+1;
x = i-1;
y = j-1;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i-1;
y = j;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i-1;
y = j+1;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i;
y = j-1;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i;
y = j+1;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i+1;
y = j-1;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i+1;
y = j;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
x = i+1;
y = j+1;
if(in_range(x, y) && A[x][y] == A[i][j] + 1 && dist[x][y] < dist[i][j] + 1){
dfs(x, y, dist[i][j]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int c=1;
while(cin>>H>>W) {
if(H==0&&W==0)
break;
for(int i=0;i<H;i++) {
string s;
cin>>s;
for(int j=0;j<W;j++){
A[i][j]=s[j]-'A';
dist[i][j]=0;
}
}
for(int i=0;i<H;i++) {
for(int j=0;j<W;j++) {
if(A[i][j]==0&&dist[i][j]==0)
dfs(i,j,0);
}
}
int ans=0;
for(int i=0;i<H;i++) {
for(int j=0;j<W;j++) {
if(ans<dist[i][j])
ans=dist[i][j];
}
}
cout<<"Case "<<c<<": "<<ans<<"\n";
c++;
}
return 0;
}