잔매의 감시 하에 풀게 된 삼성 기출 첫 문제..
애먹은 부분이 두 군데 있었는데 queue
를 염두에 두지 않아서였다.
하나는 방향 정보를 저장할 때였고 하나는 꼬리 위치를 확인할 때였다.
방향 정보는 vector
에 넣을랬는데 어차피 시간 순으로 주어지므로
queue
를 써서 하나씩 꺼내쓸 수 있게 수정했다.
꼬리 위치는 마지막 꼬리에서 상하좌우를 확인해서
흔적이 있는 곳을 자를 꼬리의 다음 꼬리로 인식해보려 했으나
가까이에 있는 냅다 몸통을 꼬리로 인식해버릴 문제가 있어 이 또한 queue
로 해결.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int N, K, L, t = 0;
int r, c, nr, nc;
int arr[101][101] = { 0, };
queue<pair<int, string>> q;
queue<pair<int, int>> path;
void getNewRc1(int dr) {
if (dr == 0) {
nr = r - 1;
nc = c;
}
else if (dr == 1) {
nr = r;
nc = c + 1;
}
else if (dr == 2) {
nr = r + 1;
nc = c;
}
else {
nr = r;
nc = c - 1;
}
}
void solution() {
r = 1;
c = 1;
arr[r][c] = 1;
path.push({ r, c });
int dr = 1; // 0상, 1우, 2하, 3좌
while (1) {
if (!q.empty()) {
if (q.front().first == t) {
if (q.front().second == "L") dr == 0 ? dr = 3 : dr -= 1;
else dr == 3 ? dr = 0 : dr += 1;
q.pop();
}
}
getNewRc1(dr);
t++;
if (nr < 1 || nr > N || nc < 1 || nc > N) return;
if (arr[nr][nc] == 1) return;
if (arr[nr][nc] != 4) {
arr[path.front().first][path.front().second] = 0;
path.pop();
}
arr[nr][nc] = 1;
path.push({ nr, nc });
r = nr;
c = nc;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
int r, c;
for (int i = 0; i < K; i++) {
cin >> r >> c;
arr[r][c] = 4; // 사과 위치
}
cin >> L;
int x;
string ch;
for (int i = 0; i < L; i++) {
cin >> x >> ch;
q.push({ x, ch }); // 이동 정보
}
solution();
cout << t;
return 0;
}