[BOJ] 3190 뱀

Time Lapse :NONE

3190.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int ans=0;
class snake
{
public:
pair<int,int> position;
int go;

snake(int a,int b, int w) :go(w)
{
position = make_pair(a,b);
}
};

vector<pair<int,int>> body;
vector<pair<int,int>> apples;
vector<pair<int,char>> moves;
int N,K,L;
snake s(1,1,1);

void move()
{
while(1) {
ans++;
pair<int, int> tail = body.at(0);
if (s.go == 0)
s.position.second--;
else if (s.go == 1)
s.position.first++;
else if (s.go == 2)
s.position.second++;
else
s.position.first--;


if (!moves.empty()) {
if (moves.at(0).first == ans) {
if (moves.at(0).second == 'L')
s.go = (s.go + 3) % 4;
else
s.go = (s.go + 1) % 4;

moves.erase(moves.begin());
}
}

if (s.position.first <= 0 || s.position.first > N || s.position.second <= 0 || s.position.second > N)
return;
vector<pair<int, int>>::iterator bodyit;
for (bodyit = body.begin(); bodyit != body.end(); bodyit++) {
if (bodyit->first == s.position.first && bodyit->second == s.position.second)
return;
}

body.push_back(s.position);

vector<pair<int, int>>::iterator appleit;
if(!apples.empty()){
for (appleit = apples.begin(); appleit != apples.end(); appleit++) {
if (appleit->first == s.position.first && appleit->second == s.position.second) {
apples.erase(appleit);
break;
}
if (*appleit == apples.back()) {
body.erase(body.begin());
}
}
} else
body.erase(body.begin());
}
}

int main(void)
{
cin>>N;
cin>>K;
for(int i=0;i<K;i++)
{
int x, y;
cin>>y>>x;
apples.push_back(make_pair(x,y));
}
cin>>L;
for(int i=0;i<L;i++)
{
int s;
char m;
cin>>s>>m;
moves.push_back(make_pair(s,m));
}

body.push_back(s.position);
move();
cout<<ans<<endl;

}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/3190/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.