https://www.acmicpc.net/problem/13901
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
struct MOVE { int x, y; };
MOVE mv[4] = { {-1,0}, {1,0}, {0,-1}, {0,1} }; //위, 아래, 왼쪽, 오른쪽
const int MAX = 1002;
int map[MAX][MAX];
int r, c;
int visited[MAX][MAX];
pair<int, int> loc;
vector<int> dir;
int dir_loc = 0;
int main(void) {
cin >> r >> c;
memset(map, -1, sizeof(map));
memset(visited, -1, sizeof(visited));
int num;
cin >> num;
for (int i = 0; i < num; i++) {
int ii, jj;
cin >> ii >> jj;
map[ii][jj] = 1;
}
cin >> loc.first >> loc.second;
for (int i = 0; i < 4; i++) {
int d;
cin >> d;
dir.push_back(d-1);
}
visited[loc.first][loc.second] = 1;
while (true) {
bool flag = false;
int movei, movej;
for (int k = 0; k < 4; k++) {
movei = loc.first + mv[dir[dir_loc]].x;
movej = loc.second + mv[dir[dir_loc]].y;
if (!(movei >= 0 && movei < r && movej >= 0 && movej < c)) {
dir_loc++;
if (dir_loc == 4) dir_loc = 0;
continue;
}
else if (map[movei][movej] == 1) {
dir_loc++;
if (dir_loc == 4) dir_loc = 0;
continue;
}
else if (visited[movei][movej] == 1) {
dir_loc++;
if (dir_loc == 4) dir_loc = 0;
continue;
}
flag = true;
break;
}
if (flag == true) {
visited[movei][movej] = 1;
loc = make_pair(movei, movej);
}
else {
cout << loc.first << " " << loc.second << endl;
break;
}
}
}
'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글
말이 되고픈 원숭이 C++ (0) | 2022.04.11 |
---|---|
2048(Hard) C++ (0) | 2022.04.07 |
보이저 1호 (0) | 2022.04.06 |
거북이 C++ (0) | 2022.04.05 |
킹 C++ (0) | 2022.03.08 |