-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABCPATH.cpp
More file actions
60 lines (56 loc) · 1.1 KB
/
ABCPATH.cpp
File metadata and controls
60 lines (56 loc) · 1.1 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
#include<bits/stdc++.h>
using namespace std;
int h=51,w=51;
char a[51][51]={'0'};
bool inside(int x,int y){
if(x<h && y<w)
return true;
return false;
}
int mx=0;
int dfs(int i,int j,char a[][51],bool visited[][51],int count){
visited[i][j]=true;
int dy[] = {0, 0, -1, 1, 1, -1, 1, -1};
int dx[] = {-1, 1, 0, 0, 1, 1, -1, -1};
char ch=a[i][j]+1;
if(count>mx)
mx=count;
for(int k=0;k<8;k++){
int x=i+dx[k];
int y=j+dy[k];
if(inside(x,y) && a[x][y]!='0'&& a[x][y]==ch && visited[x][y]==false ){
visited[x][y]=true;
dfs(x,y,a,visited,count+1);
}
}
return mx;
}
int main(){
cin>>h>>w;
char z;
int num=1;
while(h!=0 && w!=0){
vector <pair<int ,int> > v;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>z;
a[i][j]=z;
if(a[i][j]=='A')
v.push_back(make_pair(i,j));
}
}
vector <pair<int ,int> >::iterator it;
int max=0;
for(it=v.begin();it!=v.end();it++){
bool visited[51][51]={false};
mx=0;
int x=dfs(it->first,it->second,a,visited,1);
if(x>max)
max=x;
}
cout<<"Case "<<num<<": "<< max<<endl;
num++;
cin>>h>>w;
}
return 0;
}