#include <iostream>
#include <vector>
#include <set>
#include <queue>
using namespace std;
int R;
int C;
vector<int> edge[1002*1002];
vector<int> dist[1002*1002];
vector<int> blocked(1002*1002, 1);
vector<int> go_up(1002*1002, -1);
vector<int> go_down(1002*1002, -1);
vector<int> go_left(1002*1002, -1);
vector<int> go_right(1002*1002, -1);
vector<int> min_dist(1002*1002, 1e9);
vector<int> visit(1002*1002, 0);
int start;
int cake;
int u, v, w;
int pos(int r, int c)
{
return (C+2)*r + c;
}
int pair_dist(int x, int y)
{
return abs( (x % (C+2)) - (y % (C+2)) ) + abs( (x / (C+2)) - (y / (C+2)) );
}
int main()
{
//Input
cin >> R >> C;
char c;
for(int i = 1; i <= R; i++)
{
for(int j = 1; j <= C; j++)
{
cin >> c;
if(c == '#') continue;
blocked[pos(i, j)] = 0;
if(c == 'S') start = pos(i, j);
else if(c == 'C') cake = pos(i, j);
}
}
for(int i = 0; i < 1002*1002; i++)
{
go_up[i] = go_left[i] = go_down[i] = go_right[i] = i;
}
for(int i = 0; i < 1002*1002; i++)
{
if(blocked[i]) continue;
go_up[i] = go_up[i - (C+2)];
go_left[i] = go_left[i-1];
}
for(int i = 1002*1002 - 1; i >= 0; i--)
{
if(blocked[i]) continue;
go_down[i] = go_down[i + (C+2)];
go_right[i] = go_right[i+1];
}
for(int i = 1; i <= R; i++)
{
for(int j = 1; j <= C; j++)
{
u = pos(i, j); //try swapping the two blocks of code.
if(blocked[u]) continue;
for(int v: {u-1, u+1, u-(C+2), u+(C+2)})
{
if(blocked[v]) continue;
edge[u].push_back(v);
dist[u].push_back(1);
}
w = 1e9;
for(int q: {go_left[u], go_right[u], go_up[u], go_down[u]})
{
w = min(w, pair_dist(u, q) - 1);
}
for(int q: {go_left[u] + 1, go_right[u] - 1, go_up[u] + (C+2), go_down[u] - (C+2)} )
{
if(pair_dist(u, q) > w+1)
{
edge[u].push_back(q);
dist[u].push_back(w+1);
}
}
// cout << "u: " << u << '\n';
// for(int g = 0; g < edge[u].size(); g++)
// {
// cout << edge[u][g] << ' ' << dist[u][g] << '\n';
// }
// cout << "________________________________\n";
}
}
min_dist[start] = 0;
vector<int> dist_pos[R*C];
dist_pos[0].push_back(start);
for(int i = 0; i < R*C; i++)
{
for(int u: dist_pos[i])
{
if(visit[u]) continue;
visit[u] = 1;
for(int j = 0; j < edge[u].size(); j++)
{
v = edge[u][j];
w = dist[u][j];
if(min_dist[v] > min_dist[u] + w && R*C > min_dist[u] + w)
{
min_dist[v] = min_dist[u] + w;
dist_pos[ min_dist[v] ].push_back(v);
}
}
}
}
cout << min_dist[cake] << '\n';
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:157:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | for(int j = 0; j < edge[u].size(); j++)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
74988 KB |
Output is correct |
2 |
Correct |
53 ms |
74988 KB |
Output is correct |
3 |
Correct |
58 ms |
74988 KB |
Output is correct |
4 |
Correct |
51 ms |
74988 KB |
Output is correct |
5 |
Correct |
49 ms |
74988 KB |
Output is correct |
6 |
Correct |
51 ms |
74988 KB |
Output is correct |
7 |
Correct |
49 ms |
74936 KB |
Output is correct |
8 |
Correct |
59 ms |
75060 KB |
Output is correct |
9 |
Correct |
59 ms |
74988 KB |
Output is correct |
10 |
Correct |
53 ms |
74988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
75116 KB |
Output is correct |
2 |
Correct |
53 ms |
74988 KB |
Output is correct |
3 |
Correct |
56 ms |
74988 KB |
Output is correct |
4 |
Correct |
51 ms |
74988 KB |
Output is correct |
5 |
Correct |
61 ms |
74988 KB |
Output is correct |
6 |
Correct |
55 ms |
74988 KB |
Output is correct |
7 |
Correct |
54 ms |
75116 KB |
Output is correct |
8 |
Correct |
51 ms |
75052 KB |
Output is correct |
9 |
Correct |
63 ms |
75232 KB |
Output is correct |
10 |
Correct |
56 ms |
75244 KB |
Output is correct |
11 |
Correct |
52 ms |
75116 KB |
Output is correct |
12 |
Correct |
56 ms |
75116 KB |
Output is correct |
13 |
Correct |
53 ms |
75116 KB |
Output is correct |
14 |
Correct |
55 ms |
74988 KB |
Output is correct |
15 |
Correct |
54 ms |
75244 KB |
Output is correct |
16 |
Correct |
52 ms |
74988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
74988 KB |
Output is correct |
2 |
Correct |
54 ms |
75004 KB |
Output is correct |
3 |
Correct |
52 ms |
74988 KB |
Output is correct |
4 |
Correct |
59 ms |
74988 KB |
Output is correct |
5 |
Correct |
70 ms |
77548 KB |
Output is correct |
6 |
Correct |
67 ms |
77676 KB |
Output is correct |
7 |
Correct |
70 ms |
77932 KB |
Output is correct |
8 |
Correct |
67 ms |
78444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
74988 KB |
Output is correct |
2 |
Correct |
50 ms |
74988 KB |
Output is correct |
3 |
Correct |
53 ms |
74988 KB |
Output is correct |
4 |
Correct |
59 ms |
74988 KB |
Output is correct |
5 |
Correct |
52 ms |
74988 KB |
Output is correct |
6 |
Correct |
52 ms |
74988 KB |
Output is correct |
7 |
Correct |
56 ms |
75116 KB |
Output is correct |
8 |
Correct |
54 ms |
74988 KB |
Output is correct |
9 |
Correct |
59 ms |
75244 KB |
Output is correct |
10 |
Correct |
54 ms |
75244 KB |
Output is correct |
11 |
Correct |
52 ms |
75116 KB |
Output is correct |
12 |
Correct |
58 ms |
75116 KB |
Output is correct |
13 |
Correct |
55 ms |
75116 KB |
Output is correct |
14 |
Correct |
67 ms |
77548 KB |
Output is correct |
15 |
Correct |
68 ms |
77644 KB |
Output is correct |
16 |
Correct |
70 ms |
77940 KB |
Output is correct |
17 |
Correct |
69 ms |
78060 KB |
Output is correct |
18 |
Correct |
86 ms |
78700 KB |
Output is correct |
19 |
Correct |
88 ms |
79852 KB |
Output is correct |
20 |
Correct |
79 ms |
79892 KB |
Output is correct |
21 |
Correct |
67 ms |
77676 KB |
Output is correct |
22 |
Correct |
67 ms |
77544 KB |
Output is correct |
23 |
Correct |
67 ms |
77676 KB |
Output is correct |
24 |
Correct |
89 ms |
79852 KB |
Output is correct |
25 |
Correct |
53 ms |
74988 KB |
Output is correct |
26 |
Correct |
57 ms |
75244 KB |
Output is correct |
27 |
Correct |
52 ms |
74988 KB |
Output is correct |
28 |
Correct |
67 ms |
78444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
75116 KB |
Output is correct |
2 |
Correct |
52 ms |
74988 KB |
Output is correct |
3 |
Correct |
55 ms |
75116 KB |
Output is correct |
4 |
Correct |
55 ms |
74988 KB |
Output is correct |
5 |
Correct |
52 ms |
74988 KB |
Output is correct |
6 |
Correct |
59 ms |
74988 KB |
Output is correct |
7 |
Correct |
57 ms |
74988 KB |
Output is correct |
8 |
Correct |
54 ms |
74988 KB |
Output is correct |
9 |
Correct |
55 ms |
75244 KB |
Output is correct |
10 |
Correct |
53 ms |
75244 KB |
Output is correct |
11 |
Correct |
51 ms |
75116 KB |
Output is correct |
12 |
Correct |
54 ms |
75208 KB |
Output is correct |
13 |
Correct |
53 ms |
75116 KB |
Output is correct |
14 |
Correct |
71 ms |
77548 KB |
Output is correct |
15 |
Correct |
65 ms |
77676 KB |
Output is correct |
16 |
Correct |
67 ms |
78060 KB |
Output is correct |
17 |
Correct |
69 ms |
78060 KB |
Output is correct |
18 |
Correct |
74 ms |
78700 KB |
Output is correct |
19 |
Correct |
78 ms |
79852 KB |
Output is correct |
20 |
Correct |
80 ms |
79872 KB |
Output is correct |
21 |
Correct |
68 ms |
77676 KB |
Output is correct |
22 |
Correct |
74 ms |
77572 KB |
Output is correct |
23 |
Correct |
75 ms |
77676 KB |
Output is correct |
24 |
Correct |
528 ms |
157640 KB |
Output is correct |
25 |
Correct |
859 ms |
198056 KB |
Output is correct |
26 |
Correct |
746 ms |
198960 KB |
Output is correct |
27 |
Correct |
751 ms |
198252 KB |
Output is correct |
28 |
Correct |
435 ms |
141164 KB |
Output is correct |
29 |
Correct |
440 ms |
141092 KB |
Output is correct |
30 |
Correct |
455 ms |
142592 KB |
Output is correct |
31 |
Correct |
77 ms |
79852 KB |
Output is correct |
32 |
Correct |
752 ms |
198508 KB |
Output is correct |
33 |
Correct |
52 ms |
74988 KB |
Output is correct |
34 |
Correct |
54 ms |
75244 KB |
Output is correct |
35 |
Correct |
569 ms |
165740 KB |
Output is correct |
36 |
Correct |
52 ms |
74988 KB |
Output is correct |
37 |
Correct |
66 ms |
78572 KB |
Output is correct |
38 |
Correct |
419 ms |
162028 KB |
Output is correct |
39 |
Correct |
374 ms |
133356 KB |
Output is correct |