-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ_1987_알파벳.cpp
More file actions
49 lines (44 loc) · 1.04 KB
/
BJ_1987_알파벳.cpp
File metadata and controls
49 lines (44 loc) · 1.04 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
#include <iostream>
using namespace std;
bool alpha[26];
char matrix[20][20];
bool visited[20][20];
int max = 0;
void DFS(int x, int y, int nn, int R, int C);
int main() {
int R, C;
int i, j;
string s;
// 1. ÀÔ·Â
cin >> R >> C;
for (i = 0; i < R; i++) {
cin >> s;
for (j = 0; j < C; j++) {
matrix[i][j] = s[j];
}
}
// 2. Ãִ밪ã±â
DFS(0, 0, 1, R, C);
cout << ::max;
}
void DFS(int x, int y, int nn, int R, int C) {
int num = nn;
alpha[matrix[x][y] - 'A'] = true;
visited[x][y] = true;
if (::max < num) ::max = num;
if (::max >= 26) return;
if (x - 1 >= 0 && !alpha[matrix[x - 1][y]-'A'] && !visited[x - 1][y]) {
DFS(x - 1, y, num + 1, R, C);
}
if (x + 1 < R && !alpha[matrix[x + 1][y] - 'A'] && !visited[x + 1][y]) {
DFS(x + 1, y, num + 1, R, C);
}
if (y - 1 >= 0 && !alpha[matrix[x][y - 1] - 'A'] && !visited[x][y-1]) {
DFS(x, y - 1 , num + 1, R, C);
}
if (y + 1 < C && !alpha[matrix[x][y + 1] - 'A'] && !visited[x][y + 1]) {
DFS(x, y + 1, num + 1, R, C);
}
alpha[matrix[x][y] - 'A'] = false;
visited[x][y] = false;
}