Skip to content

Commit a6c1efc

Browse files
authored
[BOJ] 1987 알파벳 (G4)
1 parent 93d4c8b commit a6c1efc

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

박예진/7주차/260209.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// https://www.acmicpc.net/problem/1987
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
/*
7+
지금까지 지나온 모든칸 알파벳과 달라야함
8+
1, 1 시작, 최대한 몇 칸 지나갈 수 있는지
9+
*/
10+
11+
int R, C, res;
12+
char arr[21][21];
13+
bool visited[21][21];
14+
15+
const int dx[] = {1, 0, -1, 0};
16+
const int dy[] = {0, -1, 0, 1};
17+
18+
bool alpha[26];
19+
20+
void dfs(int x, int y, int cnt){
21+
res = max(res, cnt);
22+
23+
visited[x][y] = true;
24+
alpha[arr[x][y] - 'A'] = true;
25+
26+
for(int i = 0; i < 4; i++){
27+
int nx = x + dx[i];
28+
int ny = y + dy[i];
29+
30+
if (nx < 1 || nx > R || ny < 1 || ny > C) continue;
31+
if (!visited[nx][ny] && !alpha[arr[nx][ny] - 'A']) {
32+
dfs(nx, ny, cnt + 1);
33+
}
34+
}
35+
36+
visited[x][y] = false;
37+
alpha[arr[x][y] - 'A'] = false;
38+
}
39+
40+
int main() {
41+
ios_base::sync_with_stdio(false);
42+
cin.tie(NULL); cout.tie(NULL);
43+
44+
cin >> R >> C;
45+
for(int i = 1; i <= R; i++){
46+
for(int j = 1; j <= C; j++){
47+
cin >> arr[i][j];
48+
}
49+
}
50+
51+
dfs(1, 1, 1);
52+
cout << res;
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)