본문 바로가기

Algorithm/BAEKJOON

[BOJ] 1987번 알파벳 (C++)

728x90
반응형

https://www.acmicpc.net/problem/1987

 

1987번: 알파벳

세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으

www.acmicpc.net

 

#include <iostream>
#include <algorithm>
using namespace std;

int dx[4] = { -1, 0, 0, 1 };
int dy[4] = { 0, -1, 1, 0 };

int r, c;
char map[20][20];
int alphabet[26] = { 0, };
int max_path = 0;

void dfs(int row, int col, int find_path) {
	max_path = max(find_path, max_path);

	for (int i = 0; i < 4; ++i) {
		int nx = row + dx[i];
		int ny = col + dy[i];

		if (0 <= nx && nx < r && 0 <= ny && ny < c) {
			if (!alphabet[((int)map[nx][ny]) - 65]) {
				alphabet[((int)map[nx][ny]) - 65]++;
				dfs(nx, ny, find_path + 1);
				alphabet[((int)map[nx][ny]) - 65]--;

			}
		}
	}
}

int main() {
	cin >> r >> c;

	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			cin >> map[i][j];
		}
	}
	alphabet[((int)map[0][0]) - 65]++;
	dfs(0, 0, 1);
	cout << max_path << endl;
	return 0;
}
728x90
반응형

'Algorithm > BAEKJOON' 카테고리의 다른 글

[BOJ] 1978번 소수 찾기 (C++)  (0) 2021.11.12
[BOJ] 15651번 N과 M (3) (C++)  (0) 2021.11.11
[BOJ] 1927번 최소 힙 (C++)  (0) 2021.11.10
[BOJ] 15649번 N과 M (1) (C++)  (0) 2021.11.09
[BOJ] 11651번 좌표 정렬하기 2 (C++)  (0) 2021.11.07