# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
861187 |
2023-10-15T15:19:18 Z |
qwusha |
Portals (BOI14_portals) |
C++17 |
|
897 ms |
214812 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
typedef long double ld;
const ll inf = 1e9;
const ld eps = 1e-8;
struct vert{
int fir, sec, we;
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int stai, staj, fini, finj;
vector<vector<char>> a(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') {
stai = i;
staj = j;
} else if (a[i][j] == 'C') {
fini = i;
finj = j;
}
}
}
vector<vector<vector<vert>>> g(n, vector<vector<vert>>(m));
vector<vector<vector<pair<int, int>>>> steni(n, vector<vector<pair<int, int>>>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '#')
continue;
for (int c = 0; c < 4; c++) {
int ci = (c % 2 == 0) * (c - 1),
cj = (c % 2 == 1) * (c - 2);
if (i + ci >= 0 && i + ci < n && j + cj >= 0 && j + cj < m && a[i + ci][j + cj] != '#') {
g[i][j].push_back({i + ci, j + cj, 1});
}
}
}
}
for (int i = 0; i < n; i++) {
pair<int, int> cur = {i, 0};
for (int j = 0; j < m; j++) {
if (a[i][j] == '#')
cur = {i, j + 1};
else
steni[i][j].push_back(cur);
}
cur = {i, m - 1};
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == '#')
cur = {i, j - 1};
else
steni[i][j].push_back(cur);
}
}
for (int j = 0; j < m; j++) {
pair<int, int> cur = {0, j};
for (int i = 0; i < n; i++) {
if (a[i][j] == '#')
cur = {i + 1, j};
else
steni[i][j].push_back(cur);
}
cur = {n - 1, j};
for (int i = n - 1; i >= 0; i--) {
if (a[i][j] == '#')
cur = {i - 1, j};
else
steni[i][j].push_back(cur);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int mst = inf;
int inde = -1;
for (int qw = 0; qw < steni[i][j].size(); qw++) {
auto [si, sj] = steni[i][j][qw];
if (abs(si - i) + abs(sj - j) < mst) {
mst = abs(si - i) + abs(sj - j);
inde = qw;
}
}
for (int qw = 0; qw < steni[i][j].size(); qw++) {
auto [si, sj] = steni[i][j][qw];
if (qw != inde && abs(si - i) + abs(sj - j) > mst + 1) {
g[i][j].push_back({si, sj, mst + 1});
}
}
}
}
vector<vector<int>> dist(n, vector<int>(m, inf));
dist[stai][staj] = 0;
set<vector<int>> st = {{0, stai, staj}};
while (!st.empty()) {
auto t = st.begin();
auto qw = *t;
int d = qw[0], vi = qw[1], vj = qw[2];
st.erase(t);
for (auto [ui, uj, w] : g[vi][vj]) {
if (d + w < dist[ui][uj]) {
st.erase({dist[ui][uj], ui, uj});
dist[ui][uj] = d + w;
st.insert({d + w, ui, uj});
}
}
}
cout << dist[fini][finj] << '\n';
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:86:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int qw = 0; qw < steni[i][j].size(); qw++) {
| ~~~^~~~~~~~~~~~~~~~~~~~
portals.cpp:93:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int qw = 0; qw < steni[i][j].size(); qw++) {
| ~~~^~~~~~~~~~~~~~~~~~~~
portals.cpp:117:28: warning: 'finj' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:117:22: warning: 'fini' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:102:20: warning: 'staj' may be used uninitialized in this function [-Wmaybe-uninitialized]
102 | dist[stai][staj] = 0;
| ^
portals.cpp:102:14: warning: 'stai' may be used uninitialized in this function [-Wmaybe-uninitialized]
102 | dist[stai][staj] = 0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
14 ms |
4956 KB |
Output is correct |
6 |
Correct |
16 ms |
5212 KB |
Output is correct |
7 |
Correct |
17 ms |
5724 KB |
Output is correct |
8 |
Correct |
12 ms |
4540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
13 ms |
4956 KB |
Output is correct |
15 |
Correct |
21 ms |
5308 KB |
Output is correct |
16 |
Correct |
24 ms |
6024 KB |
Output is correct |
17 |
Correct |
17 ms |
5976 KB |
Output is correct |
18 |
Correct |
22 ms |
7260 KB |
Output is correct |
19 |
Correct |
24 ms |
8796 KB |
Output is correct |
20 |
Correct |
23 ms |
8796 KB |
Output is correct |
21 |
Correct |
14 ms |
4952 KB |
Output is correct |
22 |
Correct |
16 ms |
4956 KB |
Output is correct |
23 |
Correct |
16 ms |
5212 KB |
Output is correct |
24 |
Correct |
24 ms |
8796 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
10 ms |
4712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
14 ms |
4924 KB |
Output is correct |
15 |
Correct |
17 ms |
5212 KB |
Output is correct |
16 |
Correct |
18 ms |
5836 KB |
Output is correct |
17 |
Correct |
17 ms |
5904 KB |
Output is correct |
18 |
Correct |
22 ms |
7236 KB |
Output is correct |
19 |
Correct |
26 ms |
8796 KB |
Output is correct |
20 |
Correct |
23 ms |
8796 KB |
Output is correct |
21 |
Correct |
14 ms |
4952 KB |
Output is correct |
22 |
Correct |
15 ms |
4956 KB |
Output is correct |
23 |
Correct |
19 ms |
5212 KB |
Output is correct |
24 |
Correct |
597 ms |
154236 KB |
Output is correct |
25 |
Correct |
897 ms |
214812 KB |
Output is correct |
26 |
Correct |
849 ms |
213684 KB |
Output is correct |
27 |
Correct |
821 ms |
213764 KB |
Output is correct |
28 |
Correct |
466 ms |
116080 KB |
Output is correct |
29 |
Correct |
510 ms |
117900 KB |
Output is correct |
30 |
Correct |
530 ms |
120912 KB |
Output is correct |
31 |
Correct |
24 ms |
8796 KB |
Output is correct |
32 |
Correct |
862 ms |
213016 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
808 KB |
Output is correct |
35 |
Correct |
594 ms |
161364 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
11 ms |
4776 KB |
Output is correct |
38 |
Correct |
377 ms |
108536 KB |
Output is correct |
39 |
Correct |
369 ms |
109136 KB |
Output is correct |