# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
943310 |
2024-03-11T10:31:26 Z |
itslq |
Portals (BOI14_portals) |
C++17 |
|
335 ms |
44100 KB |
#include <bits/stdc++.h>
using namespace std;
int R, C;
const pair<int, int> dirs[4] = {make_pair(0, 1), make_pair(0, -1), make_pair(1, 0), make_pair(-1, 0)};
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> tocheck;
//queue<pair<pair<int, int>, int>> tocheck;
bool visited[1000][1000], grid[1000][1000];
int LA[1000][1000], RA[1000][1000], UA[1000][1000], DA[1000][1000];
void add(int x, int y, int n) {
//cout << R << " " << C << endl;
// cout << x << " " << y << " " << n << endl;
// cout << grid[x][y] << " " << visited[x][y] << endl;
if (x < 0 || y < 0 || x >= R || y >= C || grid[x][y] || visited[x][y]) return;
tocheck.emplace(n + 1, make_pair(x, y));
//cout << x << " " << y << " " << n << endl;
//cout << x << " " << y << " " << n + 1 << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int P = 0, si, sj, ei, ej, ni, nj, ci, cj, dist;
cin >> R >> C;
string row;
for (int i = 0; i < R; i++) {
memset(visited[i], 0, sizeof(visited[i]));
P = 0;
cin >> row;
for (int j = 0; j < C; j++) {
LA[i][j] = P;
switch (row[j]) {
case '.':
grid[i][j] = 0;
break;
case '#':
grid[i][j] = 1;
P = j + 1;
break;
case 'S':
si = i;
sj = j;
grid[i][j] = 0;
break;
case 'C':
ei = i;
ej = j;
grid[i][j] = 0;
break;
}
}
P = C - 1;
for (int j = C - 1; j >= 0; j--) {
RA[i][j] = P;
if (grid[i][j]) P = j - 1;
}
}
for (int j = 0; j < C; j++) {
P = 0;
for (int i = 0; i < R; i++) {
UA[i][j] = P;
if (grid[i][j]) P = i + 1;
}
P = R - 1;
for (int i = R - 1; i >= 0; i--) {
DA[i][j] = P;
if (grid[i][j]) P = i - 1;
}
}
pair<int, pair<int, int>> curr;
add(si, sj, -1);
//return 0;
while (true) {
curr = tocheck.top();
tocheck.pop();
ci = curr.second.first;
cj = curr.second.second;
//cout << ci << " " << cj << " " << curr.first << endl;
if (visited[ci][cj]) continue;
visited[ci][cj] = 1;
//cout << ci << " " << cj << " " << curr.second << endl;
if (ci == ei && cj == ej) {
cout << curr.first;
return 0;
}
for (pair<int, int> d: dirs) add(ci + d.first, cj + d.second, curr.first);
if (cj == 0 || cj == C - 1 || ci == 0 || ci == R - 1) dist = 0;
else dist = min(min(abs(LA[ci][cj] - cj), abs(RA[ci][cj] - cj)), min(abs(UA[ci][cj] - ci), abs(DA[ci][cj] - ci)));
add(ci, LA[ci][cj], curr.first + dist);
add(ci, RA[ci][cj], curr.first + dist);
add(UA[ci][cj], cj, curr.first + dist);
add(DA[ci][cj], cj, curr.first + dist);
}
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:25:32: warning: unused variable 'ni' [-Wunused-variable]
25 | int P = 0, si, sj, ei, ej, ni, nj, ci, cj, dist;
| ^~
portals.cpp:25:36: warning: unused variable 'nj' [-Wunused-variable]
25 | int P = 0, si, sj, ei, ej, ni, nj, ci, cj, dist;
| ^~
portals.cpp:97:28: warning: 'ej' may be used uninitialized in this function [-Wmaybe-uninitialized]
97 | if (ci == ei && cj == ej) {
| ~~~^~~~~
portals.cpp:97:16: warning: 'ei' may be used uninitialized in this function [-Wmaybe-uninitialized]
97 | if (ci == ei && cj == ej) {
| ~~~^~~~~
portals.cpp:80:8: warning: 'sj' may be used uninitialized in this function [-Wmaybe-uninitialized]
80 | add(si, sj, -1);
| ~~~^~~~~~~~~~~~
portals.cpp:80:8: warning: 'si' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8656 KB |
Output is correct |
5 |
Correct |
1 ms |
8656 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10964 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
6 ms |
17244 KB |
Output is correct |
6 |
Correct |
9 ms |
17244 KB |
Output is correct |
7 |
Correct |
7 ms |
17244 KB |
Output is correct |
8 |
Correct |
4 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
11100 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
7 ms |
17240 KB |
Output is correct |
15 |
Correct |
10 ms |
17120 KB |
Output is correct |
16 |
Correct |
7 ms |
17244 KB |
Output is correct |
17 |
Correct |
9 ms |
17320 KB |
Output is correct |
18 |
Correct |
11 ms |
17240 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
11 ms |
18900 KB |
Output is correct |
21 |
Correct |
7 ms |
17096 KB |
Output is correct |
22 |
Correct |
7 ms |
17244 KB |
Output is correct |
23 |
Correct |
8 ms |
17244 KB |
Output is correct |
24 |
Correct |
11 ms |
17244 KB |
Output is correct |
25 |
Correct |
1 ms |
8668 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
5 ms |
17252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8548 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8544 KB |
Output is correct |
4 |
Correct |
1 ms |
8548 KB |
Output is correct |
5 |
Correct |
1 ms |
8548 KB |
Output is correct |
6 |
Correct |
1 ms |
8548 KB |
Output is correct |
7 |
Correct |
1 ms |
8552 KB |
Output is correct |
8 |
Correct |
1 ms |
8544 KB |
Output is correct |
9 |
Correct |
2 ms |
10856 KB |
Output is correct |
10 |
Correct |
3 ms |
11100 KB |
Output is correct |
11 |
Correct |
2 ms |
10856 KB |
Output is correct |
12 |
Correct |
2 ms |
10848 KB |
Output is correct |
13 |
Correct |
2 ms |
10856 KB |
Output is correct |
14 |
Correct |
7 ms |
17256 KB |
Output is correct |
15 |
Correct |
8 ms |
17256 KB |
Output is correct |
16 |
Correct |
7 ms |
17256 KB |
Output is correct |
17 |
Correct |
9 ms |
17256 KB |
Output is correct |
18 |
Correct |
12 ms |
17380 KB |
Output is correct |
19 |
Correct |
3 ms |
17244 KB |
Output is correct |
20 |
Correct |
12 ms |
18896 KB |
Output is correct |
21 |
Correct |
6 ms |
17244 KB |
Output is correct |
22 |
Correct |
8 ms |
17244 KB |
Output is correct |
23 |
Correct |
8 ms |
17240 KB |
Output is correct |
24 |
Correct |
282 ms |
18256 KB |
Output is correct |
25 |
Correct |
335 ms |
18796 KB |
Output is correct |
26 |
Correct |
37 ms |
25576 KB |
Output is correct |
27 |
Correct |
198 ms |
44100 KB |
Output is correct |
28 |
Correct |
108 ms |
18048 KB |
Output is correct |
29 |
Correct |
132 ms |
18012 KB |
Output is correct |
30 |
Correct |
130 ms |
18100 KB |
Output is correct |
31 |
Correct |
11 ms |
17240 KB |
Output is correct |
32 |
Correct |
258 ms |
18012 KB |
Output is correct |
33 |
Correct |
1 ms |
8540 KB |
Output is correct |
34 |
Correct |
2 ms |
10872 KB |
Output is correct |
35 |
Correct |
108 ms |
18204 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
4 ms |
17244 KB |
Output is correct |
38 |
Correct |
53 ms |
18012 KB |
Output is correct |
39 |
Correct |
57 ms |
18012 KB |
Output is correct |