https://www.acmicpc.net/problem/3987
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct MOVE { int x, y; };
MOVE mv[4] = { {-1,0}, {0,1}, {1,0}, {0,-1} }; //위, 오른쪽, 아래, 왼쪽
const int MAX = 505;
char map[MAX][MAX];
int n, m;
pair<int, int> loc;
int answer = -1;
char ans;
int visited[MAX][MAX][4];
bool CheckFinish(pair<int,int> cur) {
if (!(cur.first >= 0 && cur.first < n && cur.second >=0 && cur.second <m)) return true;
if (map[cur.first][cur.second] == 'C') return true;
return false;
}
void Move(pair<int, int> cur, int dir,int count,char cmd) {
if (visited[cur.first][cur.second][dir] == 1) {
if (answer != 2e9) {
ans = cmd;
answer = 2e9;
}
return;
}
if (CheckFinish(cur) == true) {
if (answer < count) {
answer = count;
ans = cmd;
}
return;
}
if (map[cur.first][cur.second] == '/') {
if (dir == 0) dir = 1;
else if (dir == 1) dir = 0;
else if (dir == 2) dir = 3;
else if (dir == 3) dir = 2;
}
else if (map[cur.first][cur.second] == '\\') {
if (dir == 0) dir = 3;
else if (dir == 1) dir = 2;
else if (dir == 2) dir = 1;
else if (dir == 3) dir = 0;
}
visited[cur.first][cur.second][dir] = 1;
cur.first += mv[dir].x;
cur.second += mv[dir].y;
Move(cur, dir, count + 1,cmd);
}
int main(void) {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}
cin >> loc.first >> loc.second;
loc.first -= 1;
loc.second -= 1;
memset(visited, -1, sizeof(visited));
Move(loc, 0, 0,'U');
memset(visited, -1, sizeof(visited));
Move(loc, 1, 0,'R');
memset(visited, -1, sizeof(visited));
Move(loc, 2, 0,'D');
memset(visited, -1, sizeof(visited));
Move(loc, 3, 0,'L');
cout << ans << endl;
if (answer == 2e9) cout << "Voyager" << endl;
else cout << answer << endl;
}
'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글
2048(Hard) C++ (0) | 2022.04.07 |
---|---|
로봇 C++ (0) | 2022.04.06 |
거북이 C++ (0) | 2022.04.05 |
킹 C++ (0) | 2022.03.08 |
캐슬 디펜스 C++ (0) | 2022.03.07 |