-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermes.cpp
83 lines (65 loc) · 1.62 KB
/
hermes.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
/**
* International Olympiad of Informatics 2004, Day 1 Q2
* Ananya Kumar, 2012
* NUS High School
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 1000000000
#define ORI 1005
#define MAXD 2020
typedef pair<int,int> ii;
ii prev, cur;
int prevrow[MAXD], prevcol[MAXD];
int currow[MAXD], curcol[MAXD];
int main ()
{
int N;
FILE *in = fopen("hermes.in","r");
FILE *out = fopen("hermes.out","w");
fscanf(in,"%d",&N);
cur = make_pair(ORI,ORI);
int i, j, k;
int ans = INF;
//Initialize original row and column (origin is 0, everything else INF)
for ( i = 0; i < MAXD; i++ )
{
currow[i] = INF;
curcol[i] = INF;
prevrow[i] = INF;
prevcol[i] = INF;
}
currow[ORI] = 0;
curcol[ORI] = 0;
//Read in data and perform computations
for ( j = 0; j < N; j++ )
{
fscanf(in,"%d %d",&prev.first,&prev.second);
prev.first += ORI; prev.second += ORI;
swap(cur,prev);
for ( i = 0; i < MAXD; i++ )
{
prevrow[i] = currow[i];
prevcol[i] = curcol[i];
currow[i] = INF;
curcol[i] = INF;
}
//Update new row and column from old row and column
for ( i = 0; i < MAXD; i++ )
{
currow[i] = min(currow[i], prevrow[i]+abs(cur.second-prev.second));
curcol[prev.second] = min(curcol[prev.second], prevrow[i]+abs(i-cur.first));
curcol[i] = min(curcol[i], prevcol[i]+abs(cur.first-prev.first));
currow[prev.first] = min(currow[prev.first], prevcol[i]+abs(i-cur.second));
}
}
//Scan current row and column to output minimum
for ( i = 0; i < MAXD; i++ )
{
ans = min(ans,currow[i]);
ans = min(ans,curcol[i]);
}
fprintf(out,"%d\n",ans);
}