#include <bits/stdc++.h>
using namespace std;
void solve()
{
/// JUST KEEP GOING
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 d[n][m];
int sx, sy; int ex, ey;
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[sx][sy]=0;
}
else if(grid[l][i]=='C') {
ex=l; ey=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(w>d[a][b]) continue;
if(a==ex&&b==ey) { cout<<w<<'\n'; return; }
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) {
if(grid[x][y]=='#'||d[x][y]<d[a][b]+1) continue;
d[x][y]=d[a][b]+1; pq.push({-d[x][y], {x, y}});
}
}
int p=1000000000; vector<pair<int, int>>to;
for(int l=0;l<4;l++) {
int x=a, y=b; int k=0;
while(x>=0&&y>=0&&x<n&&y<m&&grid[x][y]!='#') {
x+=nx[l], y+=ny[l]; k++;
}
x-=nx[l], y-=ny[l];
p=min(p, k); to.push_back({x, y});
}
for(auto u:to) {
int x=u.first, y=u.second;
if(d[x][y]>d[a][b]+p) {
d[x][y]=d[a][b]+p; pq.push({-d[x][y], {x, y}});
}
}
}
}
int32_t main()
{
int t; t=1;
while(t--) {
solve();
}
return 0;
}
Compilation message
portals.cpp: In function 'void solve()':
portals.cpp:26:12: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(a==ex&&b==ey) { cout<<w<<'\n'; return; }
| ~^~~~
portals.cpp:26:19: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | 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 |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 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 |
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 |
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 |
11 ms |
460 KB |
Output is correct |
10 |
Execution timed out |
1092 ms |
49676 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
10 ms |
476 KB |
Output is correct |
6 |
Correct |
15 ms |
492 KB |
Output is correct |
7 |
Correct |
10 ms |
460 KB |
Output is correct |
8 |
Correct |
10 ms |
460 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 |
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 |
11 ms |
460 KB |
Output is correct |
10 |
Execution timed out |
1083 ms |
49752 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
11 ms |
460 KB |
Output is correct |
10 |
Execution timed out |
1092 ms |
49684 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |