-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
36 lines (36 loc) · 854 Bytes
/
Copy pathmain.cpp
File metadata and controls
36 lines (36 loc) · 854 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
#include <iostream>
using namespace std;
int r, c;
int result = 1;
char map[21][21];
bool visit[26];
int dr[] = {1,-1,0,0};
int dc[] = {0,0,1,-1};
void dfs(int x, int y, int count) {
if(count > result) result = count;
for(int k=0; k<4; k++) {
int nr = x + dr[k];
int nc = y + dc[k];
if(0 <= nr && nr < r && 0 <= nc && nc < c) {
int index = map[nr][nc]-'A';
if(visit[index] == false){
visit[index] = true;
dfs(nr,nc,count+1);
visit[index] = false;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> r >> c;
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
cin >> map[i][j];
}
}
visit[map[0][0]-'A'] = true;
dfs(0,0,1);
cout << result << '\n';
return 0;
}