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>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define int long long
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mxn = 2e6 + 5, base = 972663749;
const ll mod = 911382323, inf = 1e9;
int r, c;
pii st, en;
char a[1005][1005];
int dis[1005][1005];
bool safe(int i, int j){
return(i >= 1 && j >= 1 && i <= r && j <= c && a[i][j] != '#');
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> r >> c;
forr(i, 1, r + 1){
forr(j, 1, c + 1){
cin >> a[i][j];
if(a[i][j] == 'S'){
st = make_pair(i, j);
}else if(a[i][j] == 'C'){
en = make_pair(i, j);
}
dis[i][j] = -1;
}
}
queue<pii>q; q.push(st); dis[st.fi][st.se] = 0;
while(!q.empty()){
auto [cox, coy] = q.front(); q.pop();
forr(i, 0, 4){
int nwx = cox + x[i], nwy = coy + y[i];
if(safe(nwx, nwy) && dis[nwx][nwy] == -1){
dis[nwx][nwy] = dis[cox][coy] + 1;
q.push({nwx, nwy});
}else if(!safe(nwx, nwy)){
forr(j, 0, 4){
if(i == j)continue;
int yesx = cox, yesy = coy;
while(safe(yesx + x[j], yesy + y[j])){
yesx += x[j]; yesy += y[j];
}
if(dis[yesx][yesy] == -1){
dis[yesx][yesy] = dis[cox][coy] + 1;
q.push({yesx, yesy});
}
}
}
}
}
cout << dis[en.fi][en.se];
return(0);
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | auto [cox, coy] = q.front(); q.pop();
| ^
# | 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... |