-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs1.cpp
More file actions
56 lines (43 loc) · 1 KB
/
dfs1.cpp
File metadata and controls
56 lines (43 loc) · 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
#include <iostream>
using namespace std;
char map[25][25];
int R, C;
int bag[26];
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int answer=0;
void DFS(int x, int y,int cnt) {
bag[map[x][y] - 'A'] = 1;
for (int i = 0; i < 4; i++) {
int xx, yy;
xx = x + dx[i]; //행 증가
yy = y + dy[i]; //열 증가
if ((xx >= 0 && xx <R) && (yy >=0 && yy < C) && (!bag[map[xx][yy] - 'A'])) // bag가 0이면 -->방문하지않음
{
DFS(xx, yy, cnt + 1);
}
}
bag[map[x][y] - 'A'] = 0;
if (answer < cnt) answer = cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T; //TC ->> t=3이면 세 개의 예시 만들어져야함
for (int t = 0; t < T; ++t) {
cin >> R >> C; //R >=1 C<=20
for (int j = 0; j < R; j++) {
string str;
cin >> str;
for (int i = 0; i < C; i++)
{
map[j][i] = str[i];
}
}
DFS(0,0,1);
cout << "#" << t + 1 << " " << answer << "\n"; //최댓값 index?
answer = 0; //다음꺼에선 해제
}
return 0;
}