-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1987.cpp
More file actions
31 lines (29 loc) · 786 Bytes
/
1987.cpp
File metadata and controls
31 lines (29 loc) · 786 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
#include <bits/stdc++.h>
using namespace std;
char board[21][21];
bool visited[26];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int max_depth;
int R, C;
void dfs(int r, int c, int depth) {
max_depth = max(max_depth, depth);
visited[board[r][c] - 'A'] = true;
for (int dir = 0; dir < 4; dir++) {
if (r + dx[dir] < 0 || r + dx[dir] >= R || c + dy[dir] < 0 || c + dy[dir] >= C)
continue;
if (visited[board[r + dx[dir]][c + dy[dir]] - 'A'])
continue;
dfs(r + dx[dir], c + dy[dir], depth + 1);
}
visited[board[r][c] - 'A'] = false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> R >> C;
for (int i = 0; i < R; i++)
cin >> board[i];
dfs(0, 0, 1);
cout << max_depth;
}