This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pii pair<int, pair<int,int>>
#define fi first
#define se second
using namespace std;
int main() {
int xd[4]={-1, 0, 1, 0};
int yd[4]={0, 1, 0, -1};
int r, c, xs, ys, xe, ye; cin >> r >> c;
bool cek[r][c];
memset(cek, false, sizeof(cek));
string s[r];
for (int i=0;i<r;i++) {
cin >> s[i];
int temp = s[i].find('S');
if (temp!=-1) {
xs = i;
ys = temp;
s[i][temp]='.';
}
temp = s[i].find('C');
if (temp!=-1) {
xe = i;
ye = temp;
s[i][temp]='.';
}
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, {xs, ys}});
while (!pq.empty()) {
int nil = pq.top().fi;
int xx = pq.top().se.fi;
int yy = pq.top().se.se;
pq.pop();
if (xx==xe && yy==ye) {
cout << nil << endl;
return 0;
}
if (cek[xx][yy]) continue;
cek[xx][yy]=true;
bool dinding = false;
for (int i=1;i<4;i++) {
int x2=xx+xd[i], y2=yy+yd[i];
if (x2<0||x2>=r||y2<0||y2>=c||s[x2][y2]=='#') {
dinding =true;
continue;
}
if (cek[x2][y2]) continue;
pq.push({nil+1, {x2, y2}});
}
if (!dinding) continue;
int x2=xx, y2=yy;
while (x2-1>=0 && s[x2-1][y2]!='#') x2--;
if (!cek[x2][y2]) {
pq.push({nil+1,{x2, y2}});
}
x2=xx;
while (x2+1<r && s[x2+1][y2]!='#') x2++;
if (!cek[x2][y2]) {
pq.push({nil+1,{x2, y2}});
}
x2=xx;
while (y2-1>=0 && s[x2][y2-1]!='#') y2--;
if (!cek[x2][y2]) {
pq.push({nil+1,{x2, y2}});
}
y2=yy;
while (y2+1<c && s[x2][y2+1]!='#') y2++;
if (!cek[x2][y2]) {
pq.push({nil+1,{x2, y2}});
}
}
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:41:15: warning: 'xe' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | if (xx==xe && yy==ye) {
| ~~^~~~
portals.cpp:41:25: warning: 'ye' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | if (xx==xe && yy==ye) {
| ~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |