cgy12306
[백준 BoJ] 1987 - 알파벳 본문
// https://www.acmicpc.net/problem/1987
// 알파벳
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int R, C, ans = 0;
char map[22][22];
bool visited[27];
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0 , 0, 1, -1 };
void dfs(int x, int y, int cnt) {
ans = max(ans, cnt);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= R || ny >= C) continue;
if (!visited[map[nx][ny] - 'A']) {
visited[map[nx][ny] - 'A'] = true;
dfs(nx, ny, cnt + 1);
visited[map[nx][ny] - 'A'] = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> R >> C;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cin >> map[i][j];
}
}
visited[map[0][0]-'A'] = true;
dfs(0, 0, 1);
cout << ans;
}
'Algorithm > C++' 카테고리의 다른 글
[백준 BoJ] 2589 - 보물섬 (0) | 2022.02.16 |
---|---|
[백준 BoJ] 5014 - 스타트링크 (0) | 2022.02.16 |
[백준 BoJ] 18119 - 단어 암기 (0) | 2022.02.14 |
[백준 BoJ] 16398 - 행성 연결 (0) | 2022.02.10 |
[백준 BoJ] 10423 - 전기가 필요해 (0) | 2022.02.10 |
Comments