#include <bits/stdc++.h>
using namespace std;
void solve()
{
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};
int n, m; cin>>n>>m; char grid[n][m];
int sx, sy, ex, ey; int d[n][m]; set<int>h[m], v[n];
for(int l=0;l<m;l++) {
h[l].insert(-1); h[l].insert(n);
}
for(int l=0;l<n;l++) {
v[l].insert(-1); v[l].insert(m);
}
for(int l=0;l<n;l++) {
for(int i=0;i<m;i++) {
cin>>grid[l][i]; d[l][i]=1000000000;
if(grid[l][i]=='S') { sx=l; sy=i; d[l][i]=0; }
if(grid[l][i]=='C') { ex=l; ey=i; }
if(grid[l][i]=='#') {
h[i].insert(l);
v[l].insert(i);
}
}
}
priority_queue<pair<int, pair<int, int>>>pq; pq.push({0, {sx, sy}});
while(!pq.empty()) {
int a=pq.top().second.first, b=pq.top().second.second, w=-pq.top().first; pq.pop();
if(a==ex&&b==ey) { cout<<w<<'\n'; return; }
if(d[a][b]<w) continue;
for(int l=0;l<4;l++) {
int x=a+nx[l], y=b+ny[l];
if(x<0||x>=n||y<0||y>=m||grid[x][y]=='#') continue;
if(d[x][y]>d[a][b]+1) {
d[x][y]=d[a][b]+1; pq.push({-d[x][y], {x, y}});
}
}
auto i=h[b].lower_bound(a), j=h[b].upper_bound(a), u=v[a].lower_bound(b), k=v[a].upper_bound(b); i--; u--;
int p=min({*j-a, a-*i, *k-b, b-*u});
int a1=*i+1, aa=*j-1, b1=*u+1, bb=*k-1;
if(a1>=0&&a1<n&&grid[a1][b]!='#') {
if(d[a1][b]>d[a][b]+p) {
d[a1][b]=d[a][b]+p;
pq.push({-d[a1][b], {a1, b}});
}
}
if(aa>=0&&aa<n&&grid[aa][b]!='#') {
if(d[aa][b]>d[a][b]+p) {
d[aa][b]=d[a][b]+p;
pq.push({-d[aa][b], {aa, b}});
}
}
if(b1>=0&&b1<m&&grid[a][b1]!='#') {
if(d[a][b1]>d[a][b]+p) {
d[a][b1]=d[a][b]+p;
pq.push({-d[a][b1], {a, b1}});
}
}
if(bb>=0&&bb<m&&grid[a][bb]!='#') {
if(d[a][bb]>d[a][b]+p) {
d[a][bb]=d[a][b]+p;
pq.push({-d[a][bb], {a, bb}});
}
}
}
}
int main()
{
int t; t=1;
while(t--) {
solve();
}
}
Compilation message
portals.cpp: In function 'void solve()':
portals.cpp:30:9: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | if(a==ex&&b==ey) { cout<<w<<'\n'; return; }
| ^~
portals.cpp:30:17: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | if(a==ex&&b==ey) { cout<<w<<'\n'; return; }
| ~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
280 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
16 ms |
2124 KB |
Output is correct |
6 |
Correct |
17 ms |
2044 KB |
Output is correct |
7 |
Correct |
14 ms |
1740 KB |
Output is correct |
8 |
Correct |
11 ms |
1780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
16 ms |
2128 KB |
Output is correct |
15 |
Correct |
18 ms |
1932 KB |
Output is correct |
16 |
Correct |
13 ms |
1740 KB |
Output is correct |
17 |
Correct |
17 ms |
1884 KB |
Output is correct |
18 |
Correct |
14 ms |
1404 KB |
Output is correct |
19 |
Correct |
4 ms |
460 KB |
Output is correct |
20 |
Correct |
7 ms |
556 KB |
Output is correct |
21 |
Correct |
16 ms |
2088 KB |
Output is correct |
22 |
Correct |
16 ms |
2060 KB |
Output is correct |
23 |
Correct |
18 ms |
1868 KB |
Output is correct |
24 |
Correct |
9 ms |
572 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
11 ms |
1780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
16 ms |
2124 KB |
Output is correct |
15 |
Correct |
17 ms |
1996 KB |
Output is correct |
16 |
Correct |
13 ms |
1856 KB |
Output is correct |
17 |
Correct |
16 ms |
1812 KB |
Output is correct |
18 |
Correct |
14 ms |
1372 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
7 ms |
552 KB |
Output is correct |
21 |
Correct |
15 ms |
2084 KB |
Output is correct |
22 |
Correct |
16 ms |
2064 KB |
Output is correct |
23 |
Correct |
17 ms |
1972 KB |
Output is correct |
24 |
Correct |
613 ms |
35304 KB |
Output is correct |
25 |
Correct |
296 ms |
5696 KB |
Output is correct |
26 |
Correct |
83 ms |
5452 KB |
Output is correct |
27 |
Correct |
163 ms |
5700 KB |
Output is correct |
28 |
Correct |
602 ms |
45816 KB |
Output is correct |
29 |
Correct |
630 ms |
44360 KB |
Output is correct |
30 |
Correct |
638 ms |
43324 KB |
Output is correct |
31 |
Correct |
9 ms |
460 KB |
Output is correct |
32 |
Correct |
234 ms |
5708 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
352 ms |
29044 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
11 ms |
1780 KB |
Output is correct |
38 |
Correct |
385 ms |
36864 KB |
Output is correct |
39 |
Correct |
393 ms |
52292 KB |
Output is correct |