티스토리 뷰

백준

백준 소스코드 [C++] 1987 알파벳

Hani_Levenshtein 2020. 12. 19. 18:41

www.acmicpc.net/problem/1987

 

1987번: 알파벳

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

www.acmicpc.net

백준 소스코드 [C++] 1987 알파벳

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
#define all(v) v.begin(),v.end()

int n, m;
int arr[22][22];
bool chk[22][22];
vector<bool> brr;
struct dots {
	int y, x;
};
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };
int res = 0;
void dfs(int x,int y,int k) {
	res = max(res, k);
	for(int i=0;i<4;i++)
		if(arr[x+dx[i]][y + dy[i]]!=-1)
			if (brr[arr[x + dx[i]][y + dy[i]]] == false) {
				brr[arr[x + dx[i]][y + dy[i]]] = true;
				dfs(x + dx[i], y + dy[i], k + 1);
				brr[arr[x + dx[i]][y + dy[i]]] = false;
			}
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	string s;
	memset(arr, -1, sizeof(arr));
	for (int i = 1;i <= n;i++) {
		cin >> s;
		for (int j = 1;j <= m;j++)
			arr[i][j] = s[j - 1]-'A';
	}

	brr.resize(26), brr[arr[1][1]] = true;
	dfs(1,1,1);
	cout << res;
	return 0;
}
댓글