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>
using namespace std;
const int mxn=1005;
int n,m;
char g[mxn][mxn];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool check(int x,int y){
return x>=1&&x<=n&&y>=1&&y<=m&&g[x][y]!='#';
}
int wall[mxn][mxn];
queue<pair<int,int> >q;
void bfs()
{
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;++i)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(!check(xx,yy))continue;
if(wall[xx][yy]>wall[x][y]+1)
{
wall[xx][yy]=wall[x][y]+1;
q.push({xx,yy});
}
}
}
}
vector<pair<int,int> >wadj[mxn][mxn];;
int dist[mxn][mxn];
set<pair<int,pair<int,int> > >pq;
void dijkstra(int x,int y)
{
dist[x][y]=0;
pq.insert({0,{x,y}});
while(!pq.empty())
{
pair<int,pair<int,int> > p=*pq.begin();
pq.erase(pq.begin());
int ds=p.first;
int x=p.second.first;
int y=p.second.second;
for(int i=0;i<4;++i)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(!check(xx,yy))continue;
if(dist[xx][yy]>ds+1)
{
dist[xx][yy]=ds+1;
pq.insert({dist[xx][yy],{xx,yy}});
}
}
for(int i=0;i<wadj[x][y].size();++i)
{
int xx=wadj[x][y][i].first;
int yy=wadj[x][y][i].second;
if(dist[xx][yy]>ds+wall[x][y])
{
dist[xx][yy]=ds+wall[x][y];
pq.insert({dist[xx][yy],{xx,yy}});
}
}
}
}
void clear()
{
for(int i=0;i<mxn;++i)
for(int j=0;j<mxn;++j)
{
wall[i][j]=(1e9);
dist[i][j]=(1e9);
}
}
void up(int i,int j)
{
int x=i;
while(x>=1&&g[x][j]!='#')
{
wadj[x][j].push_back({i,j});
--x;
}
}
void down(int i,int j)
{
int x=i;
while(x<=n&&g[x][j]!='#')
{
wadj[x][j].push_back({i,j});
++x;
}
}
void left(int i,int j)
{
int y=j;
while(y>=1&&g[i][y]!='#')
{
wadj[i][y].push_back({i,j});
--y;
}
}
void right(int i,int j)
{
int y=j;
while(y<=m&&g[i][y]!='#')
{
wadj[i][y].push_back({i,j});
++y;
}
}
int main()
{
clear();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
cin>>g[i][j];
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
bool ok=false;
for(int k=0;k<4;++k)
{
int x=i+dx[k];
int y=j+dy[k];
if(!check(x,y))ok=true;
}
if(ok)
{
wall[i][j]=1;
q.push({i,j});
}
}
bfs();
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(g[i][j]!='#')continue;
down(i+1,j);
up(i-1,j);
left(i,j-1);
right(i,j+1);
}
for(int i=1;i<=n;++i)
{
right(i,1);
left(i,n);
}
for(int j=1;j<=m;++j)
{
down(1,j);
up(m,j);
}
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(g[i][j]=='S')dijkstra(i,j);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(g[i][j]=='C')printf("%d\n",dist[i][j]);
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'void dijkstra(int, int)':
portals.cpp:64:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<wadj[x][y].size();++i)
^
portals.cpp: In function 'int main()':
portals.cpp:126:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
# | 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... |