https://www.acmicpc.net/problem/1987
#include <iostream>
#include <stack>
using namespace std;
char board[21][21] = {};
bool visit[21] = { false };
int Count = 0, result = 0;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
int R, C;
void dfs(int x, int y, int cnt)
{
if (cnt > result)
result = 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)
{
if (visit[board[nx][ny]-65] == false)
{
visit[board[nx][ny] - 65] = true;
dfs(nx, ny, cnt + 1);
visit[board[nx][ny] - 65] = false;
}
}
}
}
int main(){
scanf("%d %d", &R, &C);
char input;
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
scanf("%c", &input);
if (input == '\n')
--j;
board[i][j] = input;
}
}
char start = board[0][0];
visit[start - 65] = true;
dfs(0,0,1);
printf("%d", result);
return 0;
}
728x90
'코테 > 백준 문제풀이' 카테고리의 다른 글
백준12100번 2048 (Easy) (0) | 2022.04.28 |
---|---|
백준 3055 탈출 (1) | 2021.08.24 |
백준 1697 숨바꼭질 (1) | 2021.08.12 |
백준 4358 생태학 (0) | 2021.08.09 |
백준 2002 추월 (0) | 2021.08.04 |