#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});
}
}
}
}
set<int>r[mxn];
set<int>c[mxn];
int left(int x,int y)
{
set<int>::iterator it=r[x].lower_bound(y);
--it;
return *it + 1;
}
int right(int x,int y)
{
set<int>::iterator it=r[x].lower_bound(y);
return *it - 1;
}
int up(int x,int y)
{
set<int>::iterator it=c[y].lower_bound(x);
--it;
return *it + 1;
}
int down(int x,int y)
{
set<int>::iterator it=c[y].lower_bound(x);
return *it - 1;
}
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}});
}
}
int xx,yy;
yy=left(x,y);
if(dist[x][yy]>ds+wall[x][y])
{
dist[x][yy]=ds+wall[x][y];
pq.insert({dist[x][yy],{x,yy}});
}
yy=right(x,y);
if(dist[x][yy]>ds+wall[x][y])
{
dist[x][yy]=ds+wall[x][y];
pq.insert({dist[x][yy],{x,yy}});
}
xx=up(x,y);
if(dist[xx][y]>ds+wall[x][y])
{
dist[xx][y]=ds+wall[x][y];
pq.insert({dist[xx][y],{xx,y}});
}
xx=down(x,y);
if(dist[xx][y]>ds+wall[x][y])
{
dist[xx][y]=ds+wall[x][y];
pq.insert({dist[xx][y],{xx,y}});
}
}
}
void clear()
{
for(int i=0;i<mxn;++i)
for(int j=0;j<mxn;++j)
{
wall[i][j]=(1e9);
dist[i][j]=(1e9);
}
}
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<=m;++j)
if(g[i][j]=='#')
{
r[i].insert(j);
c[j].insert(i);
}
for(int i=1;i<=n;++i)
{
r[i].insert(0);
r[i].insert(m+1);
}
for(int i=1;i<=m;++i)
{
c[i].insert(0);
c[i].insert(n+1);
}
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
portals.cpp: In function 'int main()':
portals.cpp:131:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
11000 KB |
Output is correct |
2 |
Correct |
0 ms |
11000 KB |
Output is correct |
3 |
Correct |
0 ms |
11000 KB |
Output is correct |
4 |
Correct |
3 ms |
11000 KB |
Output is correct |
5 |
Correct |
0 ms |
11000 KB |
Output is correct |
6 |
Correct |
0 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
11000 KB |
Output is correct |
8 |
Correct |
0 ms |
11000 KB |
Output is correct |
9 |
Correct |
0 ms |
11000 KB |
Output is correct |
10 |
Correct |
3 ms |
11000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11000 KB |
Output is correct |
2 |
Correct |
6 ms |
11000 KB |
Output is correct |
3 |
Correct |
0 ms |
11000 KB |
Output is correct |
4 |
Correct |
3 ms |
11000 KB |
Output is correct |
5 |
Correct |
0 ms |
11000 KB |
Output is correct |
6 |
Correct |
6 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
11000 KB |
Output is correct |
8 |
Correct |
3 ms |
11000 KB |
Output is correct |
9 |
Correct |
0 ms |
11000 KB |
Output is correct |
10 |
Correct |
3 ms |
11000 KB |
Output is correct |
11 |
Correct |
0 ms |
11132 KB |
Output is correct |
12 |
Correct |
3 ms |
11132 KB |
Output is correct |
13 |
Correct |
0 ms |
11132 KB |
Output is correct |
14 |
Correct |
6 ms |
11000 KB |
Output is correct |
15 |
Correct |
3 ms |
11132 KB |
Output is correct |
16 |
Correct |
3 ms |
11000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
11000 KB |
Output is correct |
2 |
Correct |
0 ms |
11000 KB |
Output is correct |
3 |
Correct |
0 ms |
11000 KB |
Output is correct |
4 |
Correct |
6 ms |
11000 KB |
Output is correct |
5 |
Correct |
19 ms |
12716 KB |
Output is correct |
6 |
Correct |
26 ms |
12584 KB |
Output is correct |
7 |
Correct |
29 ms |
12320 KB |
Output is correct |
8 |
Correct |
16 ms |
12320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11000 KB |
Output is correct |
2 |
Correct |
0 ms |
11000 KB |
Output is correct |
3 |
Correct |
3 ms |
11000 KB |
Output is correct |
4 |
Correct |
3 ms |
11000 KB |
Output is correct |
5 |
Correct |
0 ms |
11000 KB |
Output is correct |
6 |
Correct |
6 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
11000 KB |
Output is correct |
8 |
Correct |
3 ms |
11000 KB |
Output is correct |
9 |
Correct |
3 ms |
11000 KB |
Output is correct |
10 |
Correct |
3 ms |
11000 KB |
Output is correct |
11 |
Correct |
0 ms |
11132 KB |
Output is correct |
12 |
Correct |
0 ms |
11132 KB |
Output is correct |
13 |
Correct |
3 ms |
11132 KB |
Output is correct |
14 |
Correct |
23 ms |
12716 KB |
Output is correct |
15 |
Correct |
36 ms |
12584 KB |
Output is correct |
16 |
Correct |
36 ms |
12320 KB |
Output is correct |
17 |
Correct |
23 ms |
12452 KB |
Output is correct |
18 |
Correct |
26 ms |
11924 KB |
Output is correct |
19 |
Correct |
13 ms |
11132 KB |
Output is correct |
20 |
Correct |
16 ms |
11132 KB |
Output is correct |
21 |
Correct |
13 ms |
12584 KB |
Output is correct |
22 |
Correct |
26 ms |
12584 KB |
Output is correct |
23 |
Correct |
26 ms |
12452 KB |
Output is correct |
24 |
Correct |
13 ms |
11132 KB |
Output is correct |
25 |
Correct |
3 ms |
11000 KB |
Output is correct |
26 |
Correct |
3 ms |
11132 KB |
Output is correct |
27 |
Correct |
0 ms |
11000 KB |
Output is correct |
28 |
Correct |
19 ms |
12320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11000 KB |
Output is correct |
2 |
Correct |
0 ms |
11000 KB |
Output is correct |
3 |
Correct |
3 ms |
11000 KB |
Output is correct |
4 |
Correct |
3 ms |
11000 KB |
Output is correct |
5 |
Correct |
0 ms |
11000 KB |
Output is correct |
6 |
Correct |
0 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
11000 KB |
Output is correct |
8 |
Correct |
3 ms |
11000 KB |
Output is correct |
9 |
Correct |
0 ms |
11000 KB |
Output is correct |
10 |
Correct |
0 ms |
11000 KB |
Output is correct |
11 |
Correct |
0 ms |
11132 KB |
Output is correct |
12 |
Correct |
0 ms |
11132 KB |
Output is correct |
13 |
Correct |
0 ms |
11132 KB |
Output is correct |
14 |
Correct |
26 ms |
12716 KB |
Output is correct |
15 |
Correct |
29 ms |
12584 KB |
Output is correct |
16 |
Correct |
23 ms |
12320 KB |
Output is correct |
17 |
Correct |
23 ms |
12452 KB |
Output is correct |
18 |
Correct |
26 ms |
11924 KB |
Output is correct |
19 |
Correct |
16 ms |
11132 KB |
Output is correct |
20 |
Correct |
9 ms |
11132 KB |
Output is correct |
21 |
Correct |
16 ms |
12584 KB |
Output is correct |
22 |
Correct |
23 ms |
12584 KB |
Output is correct |
23 |
Correct |
29 ms |
12452 KB |
Output is correct |
24 |
Correct |
953 ms |
41208 KB |
Output is correct |
25 |
Correct |
533 ms |
12056 KB |
Output is correct |
26 |
Correct |
366 ms |
11528 KB |
Output is correct |
27 |
Correct |
389 ms |
11660 KB |
Output is correct |
28 |
Correct |
899 ms |
51716 KB |
Output is correct |
29 |
Correct |
983 ms |
50360 KB |
Output is correct |
30 |
Execution timed out |
1000 ms |
49196 KB |
Execution timed out |
31 |
Halted |
0 ms |
0 KB |
- |