-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio.cpp
77 lines (70 loc) · 1.99 KB
/
radio.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
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
struct cr {
int x, y;
int dist(const cr& c) {
return pow(x-c.x,2) + pow(y-c.y,2);
}
cr incr(char c) {
cr n = *this;
if (c == 'N') n.y++;
else if (c == 'S') n.y--;
else if (c == 'W') n.x--;
else if (c == 'E') n.x++;
return n;
}
};
int main() {
ifstream fin("radio.in");
ofstream fout("radio.out");
int n, m; fin >> n >> m;
cr f, b;
fin >> f.x >> f.y;
fin >> b.x >> b.y;
string fp, bp;
fin >> fp >> bp;
cr ftemp = f, btemp = b;
vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
vector<vector<pair<cr,cr>>> cors(n+1, vector<pair<cr,cr>>(m+1, {f,b}));
for (int i=0; i<=n; i++) {
if (i>=1) {
cors[i][0].first = cors[i-1][0].first.incr(fp[i-1]);
cors[i][0].second = cors[i-1][0].second;
}
for (int j=0; j<=m; j++) {
if (j>=1) {
cors[i][j].first = cors[i][j-1].first;
cors[i][j].second = cors[i][j-1].second.incr(bp[j-1]);
}
}
}
// for (auto v : cors) {
// for (auto pi : v) {
// cout << "[(" << pi.first.x << ',' << pi.first.y << ")(" << pi.second.x << ',' << pi.second.y << ")] ";
// }
// cout << '\n';
// }
for (int i=1; i<=n; i++) {
dp[i][0] += cors[i][0].first.dist(cors[i][0].second) + dp[i-1][0];
}
for (int j=1; j<=m; j++) {
dp[0][j] += cors[0][j].first.dist(cors[0][j].second) + dp[0][j-1];
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
dp[i][j] += cors[i][j].first.dist(cors[i][j].second);
dp[i][j] += min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1]));
}
}
fout << dp[n][m];
// for (int i=0; i<=n; i++) {
// for (int j=0; j<=m; j++) {
// cout << dp[i][j] << " ";
// }
// cout << '\n';
// }
}