반응형
-문제
-코드
#include<iostream>
using namespace std;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
int ch[26], r, c, res=0;
char map[20][20];
void DFS(int x, int y, int cnt)
{
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || yy < 0 || xx >= r || yy >= c) continue;
if (ch[map[xx][yy] - 'A'] == 0)
{
ch[map[xx][yy] - 'A'] = 1;
DFS(xx, yy, cnt+1);
ch[map[xx][yy] - 'A'] = 0;
}
}
if (res < cnt) res = cnt;
}
int main()
{
cin >> r >> c;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> map[i][j];
}
}
ch[map[0][0] - 'A'] = 1;
DFS(0,0,1);
printf("%d", res);
return 0;
}
반응형
'이론 > 코딩테스트' 카테고리의 다른 글
[백준] 1715번 카드 정렬하기 (그리디 알고리즘, 우선순위큐) (0) | 2022.08.09 |
---|---|
[백준] 1520번 내리막 길 (DFS + DP) (0) | 2022.08.08 |
[백준] 2583번 영역 구하기 (BFS) (0) | 2022.08.08 |
[백준] 16953번 A -> B (그리디 알고리즘) (0) | 2022.08.07 |
[백준] 2309번 일곱 난쟁이 (투포인터 알고리즘) (0) | 2022.08.07 |