-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.cpp
More file actions
64 lines (51 loc) · 1.24 KB
/
day3.cpp
File metadata and controls
64 lines (51 loc) · 1.24 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
#include <fstream>
#include <map>
#define x first
#define y second
using namespace std;
ifstream fin("aoc.in");
ofstream fout("aoc.out");
int n,m,i,j,x,y,d,nr,sol,cnt;
char c[2000];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
map <pair<int,int>,int > v;
int main(){
sol=1000000;
x=0, y=0, cnt=1;
fin>>c;
for(i=0;c[i]!=0;i++){
if(c[i]=='U') d=0; if(c[i]=='R') d=1;
if(c[i]=='D') d=2; if(c[i]=='L') d=3;
i++;
nr=0;
while(c[i]>='0' && c[i]<='9'){
nr=nr*10+c[i]-'0';
i++;
}
for(j=1;j<=nr;j++,cnt++){
x+=dx[d]; y+=dy[d];
if(v[make_pair(x,y)]==0)
v[make_pair(x,y)]=cnt;
}
}
x=0, y=0, cnt=1;
fin>>c;
for(i=0;c[i]!=0;i++){
if(c[i]=='U') d=0; if(c[i]=='R') d=1;
if(c[i]=='D') d=2; if(c[i]=='L') d=3;
i++;
nr=0;
while(c[i]>='0' && c[i]<='9'){
nr=nr*10+c[i]-'0';
i++;
}
for(j=1;j<=nr;j++,cnt++){
x+=dx[d]; y+=dy[d];
if(v[make_pair(x,y)]!=0)
sol=min(sol,cnt+v[make_pair(x,y)]);
}
}
fout<<sol;
return 0;
}