[백준 1987] 알파벳

rhkr9080·2023년 7월 7일
0

BOJ(백준)

목록 보기
12/19

문제링크 : https://www.acmicpc.net/problem/1987

💻 문제 풀이 : C++

#include <iostream>
#include <vector>

#define MAP_SIZE 25

using namespace std;

struct Coord {
	int row;
	int col;
};

int R, C, answer;
char MAP[MAP_SIZE][MAP_SIZE];
int visited[MAP_SIZE][MAP_SIZE];
int dma[100];

int dr[] = { 0, 0, -1, 1 };
int dc[] = { -1, 1, 0, 0 };

int my_max(int a, int b)
{
	return a > b ? a : b;
}

void dfs(int now_row, int now_col, int cnt)
{
	answer = my_max(answer, cnt);

	for (int i = 0; i < 4; i++)
	{
		int next_row = now_row + dr[i];
		int next_col = now_col + dc[i];
		if (next_row < 0 || next_col < 0 || next_row >= R || next_col >= C) continue;
		if (dma[MAP[next_row][next_col]] == 1) continue;
		if (visited[next_row][next_col] == 1) continue;
		visited[next_row][next_col] = 1;
		dma[MAP[next_row][next_col]] = 1;
		cnt += 1;
		dfs(next_row, next_col, cnt);
		visited[next_row][next_col] = 0;
		dma[MAP[next_row][next_col]] = 0;
		cnt -= 1;
	}
}

int main()
{
	cin >> R >> C;
	for (int i = 0; i < R; i++)
		for (int j = 0; j < C; j++)
			cin >> MAP[i][j];

	dma[MAP[0][0]] = 1;
	visited[0][0] = 1;
	dfs(0, 0, 0);

	cout << answer+1 << endl;

	return 0;
}

📌 memo 😊


ref)

profile
공부방

0개의 댓글