-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10875-뱀.cpp
More file actions
198 lines (169 loc) · 3.88 KB
/
Copy path10875-뱀.cpp
File metadata and controls
198 lines (169 loc) · 3.88 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef struct Data{
int sy,sx,ey,ex;
int dm;
}d;
int L,N;
vector<d> vst;
vector<pair<int, char> > moving;
int mx[4]={1,0,-1,0};
int my[4]={0,1,0,-1};
long long ret=0;
int y=0,x=0;
int m=0;
bool chk(int ny, int nx){//이동 가능한 칸수를 반환
int ty1,tx1,ty2,tx2,cnt;
bool r=true;
switch (m)
{
case 0:
ty1=ty2=y;
tx1=x;
tx2=nx;
cnt=nx-x;
break;
case 2:
ty1=ty2=y;
tx1=nx;
tx2=x;
cnt=x-nx;
break;
case 1:
tx1=tx2=x;
ty1=y;
ty2=ny;
cnt=ny-y;
break;
case 3:
tx1=tx2=x;
ty1=ny;
ty2=y;
cnt=y-ny;
break;
}
for(int i=vst.size()-2;i>=0;i--){//이전 몸통의 경우 이어서 늘어나므로 부딪힐 일이 없음
int sy=vst[i].sy;
int sx=vst[i].sx;
int ey=vst[i].ey;
int ex=vst[i].ex;
if(sy==ey){//몸통이 가로로 있음
if(m==0||m==2){//가로 가로
if(sy==ty1){
if((tx1<=ex)&&(tx2>=sx)){
if(m==0)
cnt=min(cnt,sx-tx1);
else
cnt=min(cnt,tx2-ex);
r=false;
}
}
}
else{//가로 세로
if(tx1>=sx&&tx1<=ex){
if(ty1<=sy&&ty2>=sy){
if(m==1)
cnt=min(cnt,sy-ty1);
else
cnt=min(cnt,ty2-sy);
r=false;
}
}
}
}
else{
if(m==1||m==3){//세로 세로
if(sx==tx1){
if((ty1<=ey)&&(ty2>=sy)){
if(m==1)
cnt=min(cnt,sy-ty1);
else
cnt=min(cnt,ty2-ey);
r=false;
}
}
}
else{//세로 가로
if(ty1>=sy&&ty1<=ey){
if(tx1<=sx&&tx2>=sx){
if(m==0)
cnt=min(cnt,sx-tx1);
else
cnt=min(cnt,tx2-sx);
r=false;
}
}
}
}
}
if(ty1<-L||tx1<-L||ty2>L||tx2>L){//그리드를 벗어낫는지 체크
switch (m)
{
case 0:
cnt = min(cnt,L+1-tx1);
break;
case 1:
cnt = min(cnt,L+1-ty1);
break;
case 2:
cnt = min(cnt,L+1+tx2);
break;
case 3:
cnt = min(cnt,L+1+ty2);
break;
}
r=false;
}
vst.push_back({ty1,tx1,ty2,tx2});
ret+=cnt;
return r;
}
bool chgmove(int t, char dir){
int ny = y+my[m]*t;
int nx = x+mx[m]*t;
bool tmp = chk(ny,nx);
if(!tmp){
return false;
}
if(dir=='L'){
--m;
if(m<0)
m=3;
}
else{
++m;
m%=4;
}
y=ny;
x=nx;
return true;
}
void Input(){
cin>>L>>N;
int a;
char b;
for(int i=0;i<N;i++){
cin>>a>>b;
moving.push_back({a,b});
}
moving.push_back({200'000'007,0});
}
void Solution(){
for(int i=0;i<moving.size()+1;i++){
int t = moving[i].first;
char dir = moving[i].second;
if(!chgmove(t,dir))
break;
}
cout<<ret<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Input();
Solution();
return 0;
}