-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1987.cpp
More file actions
38 lines (34 loc) · 735 Bytes
/
1987.cpp
File metadata and controls
38 lines (34 loc) · 735 Bytes
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
#include <iostream>
#include <algorithm>
using namespace std;
int dy[4]={1,-1,0,0};
int dx[4]={0,0,1,-1};
int R,C;
int ans=0;
char board[20][20]={0};
int alphabet[26]={0};
void dfs(int y,int x,int cnt){
ans=max(ans,cnt);
for(int i=0;i<4;i++){
int ny=y+dy[i];
int nx=x+dx[i];
if(ny<0 || ny>=R || nx<0 || nx>=C){continue;}
int k=(int)board[ny][nx]-65;
if(alphabet[k]){continue;}
alphabet[k]++;
dfs(ny,nx,cnt+1);
alphabet[k]--;
}
}
int main(){
cin>>R>>C;
for(int i=0;i<R;i++){
for(int j=0;j<C;j++){
cin>>board[i][j];
}
}
alphabet[(int)board[0][0]-65]++;
dfs(0,0,1);
cout<<ans<<endl;
return 0;
}