-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·94 lines (79 loc) · 2.51 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·94 lines (79 loc) · 2.51 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Distance
{
int straights = 0;
int quarter_rounds = 0;
Distance operator+(const Distance &other)
{
return {straights + other.straights, quarter_rounds + other.quarter_rounds};
}
};
bool isValidPosition(int r, int c)
{
if (r == 0 && c == 0)
return false;
return (r % 2 == 1 && c % 2 == 1) == 0;
}
double dist(const Distance &d)
{
return 5.0 * (double)d.straights + 10.0 * 3.14159265359 / 4.0 * (double)d.quarter_rounds;
}
Distance min(const Distance &a, const Distance &b)
{
if (dist(a) < dist(b))
return a;
return b;
}
int main()
{
int h, w;
cin >> h >> w;
fixed(cout);
cout.precision(9);
vector<vector<char>> GRID(h, vector<char>(w, '.'));
for (int r = 0; r < h; ++r)
for (int c = 0; c < w; ++c)
cin >> GRID[r][c];
int rows = 2 * h + 1;
int cols = 2 * w + 1;
vector<vector<Distance>> DIST(rows, vector<Distance>(cols));
for (int r = 0; r < rows; ++r)
{
for (int c = 0; c < cols; ++c)
{
if (isValidPosition(r, c))
{
if (r == 0) // on first horizontal street
{
DIST[r][c] = DIST[r][c - 1] + Distance{1, 0};
}
else if (c == 0) // on first vertical street
{
DIST[r][c] = DIST[r - 1][c] + Distance{1, 0};
}
else if (r % 2 == 0 && c % 2 == 0) // on intersection
{
DIST[r][c] = min(DIST[r][c - 1] + Distance{1, 0}, DIST[r - 1][c] + Distance{1, 0});
}
else if (r % 2 == 0) // horizontally in between intersections
{
if (r > 1 && GRID[(r - 1) / 2][c / 2] == 'O')
DIST[r][c] = min(DIST[r][c - 1] + Distance{1, 0}, DIST[r - 1][c - 1] + Distance{0, 1});
else
DIST[r][c] = DIST[r][c - 1] + Distance{1, 0};
}
else if (c % 2 == 0) // vertically in between intersections
{
if (c > 1 && GRID[r / 2][(c - 1) / 2] == 'O')
DIST[r][c] = min(DIST[r - 1][c] + Distance{1, 0}, DIST[r - 1][c - 1] + Distance{0, 1});
else
DIST[r][c] = DIST[r - 1][c] + Distance{1, 0};
}
}
}
}
cout << dist(DIST[rows - 1][cols - 1]) << endl;
}