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 fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x),end(x)
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
ll const inf = 1e9;
ll const mod = 1e9 + 7;
ld const eps = 1e-9;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
char mapa[n][m];
int sr, sc;
int er, ec;
fr(i, 0, n){
fr(j, 0, m){
cin >> mapa[i][j];
if(mapa[i][j] == 'S'){
sr = i;
sc = j;
mapa[i][j] = '.';
}
else if(mapa[i][j] == 'C'){
er = i;
ec = j;
mapa[i][j] = '.';
}
}
}
int toleft[n][m];
int toright[n][m];
fr(i, 0, n){
int last = -1;
fr(j, 0, m){
if(mapa[i][j] == '.' && last == -1){
last = j;
}
else if(mapa[i][j] == '#') last = -1;
toleft[i][j] = last;
}
last = -1;
for(int j = m - 1; j >= 0; j --){
if(mapa[i][j] =='.' && last == -1){
last = j;
}
else if(mapa[i][j] == '#') last = -1;
toright[i][j] = last;
}
}
int toup[n][m];
int todown[n][m];
fr(j, 0, m){
int last = -1;
fr(i, 0, n){
if(mapa[i][j] == '.' && last == -1){
last = i;
}
else if(mapa[i][j] == '#') last = -1;
toup[i][j] = last;
}
last = -1;
for(int i = n - 1; i >= 0; i --){
if(mapa[i][j] == '.' && last == -1){
last = i;
}
else if(mapa[i][j] == '#') last = -1;
todown[i][j] = last;
}
}
bool portal[n][m];
memset(portal, false, sizeof(portal));
fr(i, 0, n){
fr(j, 0, m){
if(i == 0 || i == n - 1 || j == 0 || j == m - 1 || mapa[i + 1][j] == '#' || mapa[i - 1][j] == '#' || mapa[i][j + 1] == '#' || mapa[i][j - 1] == '#'){
portal[i][j] = true;
}
}
}
queue<pair<int,pii> > Q;
Q.push(mp(0,mp(sr,sc)));
bool vis[n][m];
memset(vis, false, sizeof(vis));
vis[sr][sc] = true;
fr(i, 0, n){
fr(j, 0, m){
if(mapa[i][j] == '#') vis[i][j] = true;
}
}
while(!Q.empty()){
int r = Q.front().nd.st;
int c = Q.front().nd.nd;
int d = Q.front().st;
if(r == er && c == ec){
cout << d <<endl;
return 0;
}
Q.pop();
if(r + 1 < n && !vis[r + 1][c]){
vis[r + 1][c] = true;
Q.push({d + 1,{r + 1, c}});
}
if(r - 1 >= 0 && !vis[r - 1][c]){
vis[r - 1][c] = true;
Q.push({d + 1,{r - 1, c}});
}
if(c + 1 < m && !vis[r][c + 1]){
vis[r][c + 1] = true;
Q.push({d + 1,{r, c + 1}});
}
if(c - 1 >= 0 && !vis[r][c - 1]){
vis[r][c - 1] = true;
Q.push({d + 1,{r, c - 1}});
}
if(portal[r][c]){
if(toleft[r][c] != -1 && !vis[r][toleft[r][c]]){
vis[r][toleft[r][c]] = true;
Q.push(mp(d + 1,mp(r, toleft[r][c])));
}
if(toright[r][c] != -1 && !vis[r][toright[r][c]]){
vis[r][toright[r][c]] = true;
Q.push(mp(d + 1,mp(r, toright[r][c])));
}
if(toup[r][c] != -1 && !vis[toup[r][c]][c]){
vis[toup[r][c]][c] = true;
Q.push(mp(d + 1,mp(toup[r][c], c)));
}
if(todown[r][c] != -1 && !vis[todown[r][c]][c]){
vis[todown[r][c]][c] = true;
Q.push(mp(d + 1,mp(todown[r][c], c)));
}
}
}
cout << -1 << endl;
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:98:21: warning: 'sc' may be used uninitialized in this function [-Wmaybe-uninitialized]
vis[sr][sc] = true;
~~~~~~~~~~~~^~~~~~
portals.cpp:98:21: warning: 'sr' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:108:17: warning: 'er' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(r == er && c == ec){
^~
portals.cpp:108:28: warning: 'ec' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(r == er && c == ec){
~~~~~~~~^~~~~~~~~~
# | 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... |