https://www.acmicpc.net/problem/3190
✔ 알고리즘 분류: 구현, 자료구조, 시뮬레이션, 덱, 큐
✔ 사과의 위치와 뱀의 이동경로가 주어지기 때문에 최단거리/시간 같은걸 고민할 필요가 없다. 빡구현 가자.
✔ 이 문제를 풀 땐 deque을 잘 몰라서 안썼다. 그래도 deque 복습하고 가자.
#include <deque>
int main() {
deque<int> dq;
for(int i=0;i<5;i++) {
dq.push_back((i+1)*10);
}
deque<int>::iterator iter;
for(iter = dq.begin(); iter != dq.end(); iter++) {
cout<<*iter<<" ";
}
dq.push_front(1);
dq.push_back(100);
/1번째 위치에 50을 삽입
dq.insert(1,50);
//2번째 위치에 1개의 70을 삽입
dq.insert(2, 1, 50);
dq.pop_front();
dq.pop_back();
dq.erase(dq.begin()+1);
//역으로 출력하기
deque<int>::reverse_iterator rIter;
for(rIter = dq.rbegin(); rIter !=dq.rend(); rIter++)
cout<<*rIter<<' ';
}
using namespace std;
#include <iostream>
#include <vector>
int main() {
int n, k, l;
int board[101][101] = {};
int x, y, time = 0;
int headX = 1, headY = 1, d = 0;
int direction[4][2] = { 0, 1, //동
1, 0, //남
0, -1, //서
-1, 0}; //북
char c;
bool die = false;
int east = 0, west = 1, south = 2, north = 3;
vector<pair<int, char>> info;
vector<pair<int, int>> tail;
cin >> n >> k;
board[headX][headY] = 1; //뱀은 1
for (int i = 0; i < k; i++) {
cin >> x >> y;
board[x][y] = 2; //사과는 2
}
cin >> l;
for (int i = 0; i < l; i++) {
cin >> x >> c;
info.push_back(make_pair(x, c));
}
tail.push_back(make_pair(headX, headY));
while (die == false) {
//시간++
time++;
//뱀이동불가지역이면
if ((headX + direction[d][0]) > n || (headY + direction[d][1]) > n ||
(headX + direction[d][0]) <= 0 || (headY + direction[d][1]) <= 0 ||
board[headX + direction[d][0]][headY + direction[d][1]] == 1) {
die = true;
continue;
}
//사과 없으면
if (board[headX + direction[d][0]][headY + direction[d][1]] != 2) {
board[tail[0].first][tail[0].second] = 0;
tail.erase(tail.begin());
}
//뱀이동
board[headX + direction[d][0]][headY + direction[d][1]] = 1;
headX += direction[d][0];
headY += direction[d][1];
tail.push_back(make_pair(headX, headY));
//방향전환
if (info.empty() ==false && info[0].first == time) {
if (info[0].second == 'L') {
d -= 1;
if (d == -1)
d = 3;
}
else {
d += 1;
if (d == 4)
d = 0;
}
info.erase(info.begin());
}
}
printf("%d\n", time);
}