-
[백준 1987] 알파벳알고리즘/백준 2019. 5. 11. 12:15
DFS 탐색을 이용해서 해결한 문제이다.
char board[20][20]; // 각각의 알파벳이 들어감
bool check[20][20]; // 방문을 했는지 체크
vector<pair<int, int> > point; // 현재 위치에서 이동할 수 있는 범위 계산 (상하좌우)
vector<char> visit; // 이미 지난 알파벳만 push! 주의할 점 !
이동하지 못할 경우라도 현재 있는 위치도 이동이
가능하므로 1을 출력해준다.
출처 : https://www.acmicpc.net/problem/1987
문제
세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다.
말은 상하좌우로 인접한 네 칸 중의 한 칸으로 이동할 수 있는데, 새로 이동한 칸에 적혀 있는 알파벳은 지금까지 지나온 모든 칸에 적혀 있는 알파벳과는 달라야 한다. 즉, 같은 알파벳이 적힌 칸을 두 번 지날 수 없다.
좌측 상단에서 시작해서, 말이 최대한 몇 칸을 지날 수 있는지를 구하는 프로그램을 작성하시오. 말이 지나는 칸은 좌측 상단의 칸도 포함된다.
입력
첫째 줄에 R과 C가 빈칸을 사이에 두고 주어진다. (1<=R,C<=20) 둘째 줄부터 R개의 줄에 걸쳐서 보드에 적혀 있는 C개의 대문자 알파벳들이 빈칸 없이 주어진다.
출력
첫째 줄에 말이 지날 수 있는 최대의 칸 수를 출력한다.
예제 입력 1
2 4 CAAB ADCB
예제 출력 1
3
소스코드1
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859#include<iostream>#include<vector>#include<utility>#include<algorithm>#include<string>using namespace std;int r, c, ans = 1;char board[20][20];bool check[20][20];vector<pair<int, int> > point;vector<char> visit;void insert() {// 상하좌우를 삽입한다.point.push_back(make_pair(-1, 0));point.push_back(make_pair(0, -1));point.push_back(make_pair(1, 0));point.push_back(make_pair(0, 1));// 알파벳을 board에 삽입for (int i = 0; i < r; i++) {for (int j = 0; j < c; j++)scanf(" %c", &board[i][j]);}}// dfs 탐색void solve(int cur_x, int cur_y, int cnt) {// 각각의 경우의 수마다 cnt이 ans보다 더 크면// ans 변경if (ans < cnt)ans = cnt;check[cur_x][cur_y] = true; // 방문 표시visit.push_back(board[cur_x][cur_y]); // 방문 했던 알파벳 pushfor (int i = 0; i < 4; i++) {int next_x = cur_x + point[i].first; // 다음 next (상하좌우)int next_y = cur_y + point[i].second;if (next_x >= 0 && next_x < r && next_y >= 0 && next_y < c && !check[next_x][next_y]) {char item = board[next_x][next_y]; // 다음 알파벳// next에 있는 알파벳을 지나쳐오지 않았고 && next에 방문하지 않았다면if (find(visit.begin(), visit.end(), item) == visit.end() && !check[next_x][next_y]) {solve(next_x, next_y, cnt + 1); // 다음 깊이로 진행한다check[next_x][next_y] = false; // 해당 경로의 깊이를 빠져나오기 때문에 => 방문하지 않았다고 표시visit.pop_back(); // 해당 알파벳도 마찬가지로 빼준다.}}}}int main(void) {cin >> r >> c;insert();solve(0, 0, 1);cout << ans;return 0;}cs 소스코드2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051#include <iostream>#include <algorithm>#include <vector>using namespace std;int R, C, board[20][20];bool alpha[26];vector<pair<int, int>> move_;int dfs(int x, int y) {int ans(0);alpha[board[x][y]] = true;for (int i = 0; i < 4; i++) {int new_x = x + move_[i].first;int new_y = y + move_[i].second;if (new_x >= 0 && new_y >= 0 && new_x < R && new_y < C && !alpha[board[new_x][new_y]]) {ans = max(ans, dfs(new_x, new_y));}}alpha[board[x][y]] = false;return ans + 1;}int main(void) {ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0);move_.push_back(make_pair(0, 1));move_.push_back(make_pair(1, 0));move_.push_back(make_pair(0, -1));move_.push_back(make_pair(-1, 0));cin >> R >> C;for (int i = 0; i < R; i++) {for (int j = 0; j < C; j++) {char c;cin >> c;board[i][j] = c - 'A';}}cout << dfs(0, 0);return 0;}cs ※ 본 글은 개인 포트폴리오 혹은 공부용으로 사용하기 때문에, 무단 복사 유포는 금지하지만, 개인 공부 용도로는 얼마든지 사용하셔도 좋습니다.
반응형'알고리즘 > 백준' 카테고리의 다른 글
[백준 3111] 검열 (0) 2019.05.11 [백준 1182] 부분수열의 합 (0) 2019.05.11 [백준 2580] 스도쿠 (4) 2019.05.08 [백준 9663] N-Queen (0) 2019.05.08