This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// Be Kind :)
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Os")
/* Bitch, where you when I was walkin'?
Now I run the game, got the whole world talkin' */
/* You never likes us anyway, fuck your friendship, I meant it */
/* The Chanel or Balenciaga, Louis and Vuitton
She know I got Fendi, Prada when I hit Milan */
/* Niggas been counting me out
I'm counting my bullets, I'm loading my clips
I'm writing down names, I'm making a list */
/* I ran away, I don't think I'm coming back home */
#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
typedef int ll;
typedef pair<ll,ll> pll;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=1e3+10, maxm=51, lg=23, mod=1e9+7, inf=1e18;
ll dx[]={1,0,-1,0};
ll dy[]={0,1,0,-1};
char bb[maxn][maxn];
ll ww[maxn][maxn],dis[maxn][maxn];
bool mk[maxn][maxn];
ll U[maxn][maxn],D[maxn][maxn],L[maxn][maxn],R[maxn][maxn];
priority_queue<pll,vector<pll>,greater<pll>> pq;
void midi(ll x,ll y,ll d){
if(dis[x][y]>d) dis[x][y]=d, pq.push({dis[x][y],x*maxn+y});
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) ww[i][j]=min({i,j,n-i+1,m-j+1});
deque<ll> q;
ll sx,sy,ex,ey;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>bb[i][j];
if(bb[i][j]=='S') sx=i,sy=j, bb[i][j]='.';
else if(bb[i][j]=='C') ex=i,ey=j, bb[i][j]='.';
else if(bb[i][j]=='#'){
ww[i][j]=0;
q.pb(i*maxn+j);
}
}
}
while(!q.empty()){
ll x=q.front()/maxn, y=q.front()%maxn; q.pop_front();
for(int k:{0,1,2,3}){
ll i=x+dx[k], j=y+dy[k];
if(bb[i][j]=='.'){
if(ww[i][j]>ww[x][y]+1) ww[i][j]=ww[x][y]+1, q.pb(i*maxn+j);
}
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
U[i][j]=(bb[i-1][j]=='.' ? U[i-1][j]:i*maxn+j);
L[i][j]=(bb[i][j-1]=='.' ? L[i][j-1]:i*maxn+j);
}
for(int i=n;i>=1;i--)for(int j=m;j>=1;j--){
D[i][j]=(bb[i+1][j]=='.' ? D[i+1][j]:i*maxn+j);
R[i][j]=(bb[i][j+1]=='.' ? R[i][j+1]:i*maxn+j);
}
memset(dis,69,sizeof(dis));
dis[sx][sy]=0;
pq.push({dis[sx][sy],sx*maxn+sy});
while(!pq.empty()){
ll x=pq.top().S/maxn, y=pq.top().S%maxn; pq.pop();
if(mk[x][y]) continue;
for(int k:{0,1,2,3}){
int i=x+dx[k], j=y+dy[k];
if(bb[i][j]=='.') midi(i,j,dis[x][y]+1);
}
midi(R[x][y]/maxn,R[x][y]%maxn,dis[x][y]+ww[x][y]);
midi(U[x][y]/maxn,U[x][y]%maxn,dis[x][y]+ww[x][y]);
midi(L[x][y]/maxn,L[x][y]%maxn,dis[x][y]+ww[x][y]);
midi(D[x][y]/maxn,D[x][y]%maxn,dis[x][y]+ww[x][y]);
}
cout<<dis[ex][ey];
return 0;
}
Compilation message (stderr)
portals.cpp:34:54: warning: overflow in implicit constant conversion [-Woverflow]
const ll maxn=1e3+10, maxm=51, lg=23, mod=1e9+7, inf=1e18;
^~~~
portals.cpp: In function 'int main()':
portals.cpp:100:18: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout<<dis[ex][ey];
^
portals.cpp:100:18: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:87:30: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq.push({dis[sx][sy],sx*maxn+sy});
~~~~~~~^~~
portals.cpp:87:25: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq.push({dis[sx][sy],sx*maxn+sy});
~~^~~~~
# | 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... |