#include<algorithm>
#include<utility>
#include<queue>
#include<iostream>
using namespace std;
char mat[1005][1005];
int ply[1005][1005];
int pry[1005][1005];
int pux[1005][1005];
int pdx[1005][1005];
int r,c;
priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > pq;
int v[1005][1005];
void make_portal()
{
for(int i=0;i<r;i++)
{
int piv=0;
for(int j=0;j<c;j++)
{
if(mat[i][j]=='#')
piv=j+1;
pry[i][j]=piv;
}
}
for(int i=0;i<r;i++)
{
int piv=c-1;
for (int j=c-1;j>=0;j--)
{
if(mat[i][j]=='#')
piv=j-1;
ply[i][j]=piv;
}
}
for(int j=0;j<c;j++)
{
int piv=0;
for(int i=0;i<r;i++)
{
if(mat[i][j]=='#')
piv=i+1;
pdx[i][j]=piv;
}
}
for(int j=0;j<c;j++)
{
int piv=r-1;
for(int i=r-1;i>=0;i--)
{
if(mat[i][j]=='#')
piv = i-1;
pux[i][j]=piv;
}
}
}
void push(int x,int y,int d)
{
if(x<0||x>=r||y<0||y>=c)
return;
if(v[x][y])
return;
if(mat[x][y]=='#')
return;
pq.push(make_pair(d,make_pair(x,y)));
}
int main()
{
cin>>r>>c;
for(int i=0;i<r;i++)
{
cin>>mat[i];
for(int j=0;j<c;j++)
{
if(mat[i][j]=='S')
{
pq.push(make_pair(0,make_pair(i,j)));
}
}
}
make_portal();
while(!pq.empty())
{
pair<int,pair<int,int> > c=pq.top();
pq.pop();
int x=c.second.first;
int y=c.second.second;
int d=c.first;
if(v[x][y])
continue;
if(mat[x][y]=='C')
{
cout<<d<<endl;
return 0;
}
v[x][y]=1;
push(x-1,y,d+1);
push(x,y-1,d+1);
push(x+1,y,d+1);
push(x,y+1,d+1);
int dist=min(abs(ply[x][y]-y),abs(y-pry[x][y]));
dist=min(dist,min(abs(pux[x][y]-x),abs(pdx[x][y]-x)));
push(x,ply[x][y],d+dist+1);
push(x,pry[x][y],d+dist+1);
push(pux[x][y],y,d+dist+1);
push(pdx[x][y],y,d+dist+1);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22732 KB |
Output is correct |
2 |
Correct |
0 ms |
22732 KB |
Output is correct |
3 |
Correct |
0 ms |
22732 KB |
Output is correct |
4 |
Correct |
0 ms |
22732 KB |
Output is correct |
5 |
Correct |
0 ms |
22732 KB |
Output is correct |
6 |
Correct |
0 ms |
22732 KB |
Output is correct |
7 |
Correct |
0 ms |
22732 KB |
Output is correct |
8 |
Correct |
0 ms |
22732 KB |
Output is correct |
9 |
Correct |
0 ms |
22732 KB |
Output is correct |
10 |
Correct |
0 ms |
22732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22732 KB |
Output is correct |
2 |
Correct |
0 ms |
22732 KB |
Output is correct |
3 |
Correct |
0 ms |
22732 KB |
Output is correct |
4 |
Correct |
0 ms |
22732 KB |
Output is correct |
5 |
Correct |
0 ms |
22732 KB |
Output is correct |
6 |
Correct |
0 ms |
22732 KB |
Output is correct |
7 |
Correct |
0 ms |
22732 KB |
Output is correct |
8 |
Correct |
0 ms |
22732 KB |
Output is correct |
9 |
Correct |
0 ms |
22732 KB |
Output is correct |
10 |
Correct |
0 ms |
22904 KB |
Output is correct |
11 |
Correct |
0 ms |
22732 KB |
Output is correct |
12 |
Correct |
0 ms |
22732 KB |
Output is correct |
13 |
Correct |
0 ms |
22732 KB |
Output is correct |
14 |
Correct |
0 ms |
22732 KB |
Output is correct |
15 |
Correct |
0 ms |
22732 KB |
Output is correct |
16 |
Correct |
0 ms |
22732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22732 KB |
Output is correct |
2 |
Correct |
0 ms |
22732 KB |
Output is correct |
3 |
Correct |
0 ms |
22732 KB |
Output is correct |
4 |
Correct |
0 ms |
22732 KB |
Output is correct |
5 |
Correct |
6 ms |
22732 KB |
Output is correct |
6 |
Correct |
13 ms |
22732 KB |
Output is correct |
7 |
Correct |
9 ms |
22732 KB |
Output is correct |
8 |
Correct |
3 ms |
22732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22732 KB |
Output is correct |
2 |
Correct |
0 ms |
22732 KB |
Output is correct |
3 |
Correct |
0 ms |
22732 KB |
Output is correct |
4 |
Correct |
0 ms |
22732 KB |
Output is correct |
5 |
Correct |
0 ms |
22732 KB |
Output is correct |
6 |
Correct |
0 ms |
22732 KB |
Output is correct |
7 |
Correct |
0 ms |
22732 KB |
Output is correct |
8 |
Correct |
0 ms |
22732 KB |
Output is correct |
9 |
Correct |
0 ms |
22732 KB |
Output is correct |
10 |
Correct |
0 ms |
22904 KB |
Output is correct |
11 |
Correct |
0 ms |
22732 KB |
Output is correct |
12 |
Correct |
0 ms |
22732 KB |
Output is correct |
13 |
Correct |
0 ms |
22732 KB |
Output is correct |
14 |
Correct |
6 ms |
22732 KB |
Output is correct |
15 |
Correct |
13 ms |
22732 KB |
Output is correct |
16 |
Correct |
9 ms |
22732 KB |
Output is correct |
17 |
Correct |
9 ms |
22732 KB |
Output is correct |
18 |
Correct |
9 ms |
22904 KB |
Output is correct |
19 |
Correct |
0 ms |
22904 KB |
Output is correct |
20 |
Correct |
13 ms |
25120 KB |
Output is correct |
21 |
Correct |
3 ms |
22732 KB |
Output is correct |
22 |
Correct |
6 ms |
22732 KB |
Output is correct |
23 |
Correct |
9 ms |
22732 KB |
Output is correct |
24 |
Correct |
13 ms |
22732 KB |
Output is correct |
25 |
Correct |
0 ms |
22732 KB |
Output is correct |
26 |
Correct |
0 ms |
22732 KB |
Output is correct |
27 |
Correct |
0 ms |
22732 KB |
Output is correct |
28 |
Correct |
6 ms |
22732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22732 KB |
Output is correct |
2 |
Correct |
0 ms |
22732 KB |
Output is correct |
3 |
Correct |
0 ms |
22732 KB |
Output is correct |
4 |
Correct |
0 ms |
22732 KB |
Output is correct |
5 |
Correct |
0 ms |
22732 KB |
Output is correct |
6 |
Correct |
0 ms |
22732 KB |
Output is correct |
7 |
Correct |
0 ms |
22732 KB |
Output is correct |
8 |
Correct |
0 ms |
22732 KB |
Output is correct |
9 |
Correct |
0 ms |
22732 KB |
Output is correct |
10 |
Correct |
0 ms |
22904 KB |
Output is correct |
11 |
Correct |
0 ms |
22732 KB |
Output is correct |
12 |
Correct |
0 ms |
22732 KB |
Output is correct |
13 |
Correct |
0 ms |
22732 KB |
Output is correct |
14 |
Correct |
6 ms |
22732 KB |
Output is correct |
15 |
Correct |
13 ms |
22732 KB |
Output is correct |
16 |
Correct |
6 ms |
22732 KB |
Output is correct |
17 |
Correct |
13 ms |
22732 KB |
Output is correct |
18 |
Correct |
9 ms |
22904 KB |
Output is correct |
19 |
Correct |
0 ms |
22904 KB |
Output is correct |
20 |
Correct |
13 ms |
25120 KB |
Output is correct |
21 |
Correct |
6 ms |
22732 KB |
Output is correct |
22 |
Correct |
3 ms |
22732 KB |
Output is correct |
23 |
Correct |
9 ms |
22732 KB |
Output is correct |
24 |
Correct |
413 ms |
23100 KB |
Output is correct |
25 |
Correct |
659 ms |
23968 KB |
Output is correct |
26 |
Correct |
96 ms |
32032 KB |
Output is correct |
27 |
Correct |
409 ms |
59680 KB |
Output is correct |
28 |
Correct |
199 ms |
22732 KB |
Output is correct |
29 |
Correct |
233 ms |
22732 KB |
Output is correct |
30 |
Correct |
249 ms |
22732 KB |
Output is correct |
31 |
Correct |
16 ms |
22732 KB |
Output is correct |
32 |
Correct |
463 ms |
22904 KB |
Output is correct |
33 |
Correct |
0 ms |
22732 KB |
Output is correct |
34 |
Correct |
0 ms |
22732 KB |
Output is correct |
35 |
Correct |
213 ms |
22904 KB |
Output is correct |
36 |
Correct |
0 ms |
22732 KB |
Output is correct |
37 |
Correct |
3 ms |
22732 KB |
Output is correct |
38 |
Correct |
156 ms |
22732 KB |
Output is correct |
39 |
Correct |
136 ms |
22732 KB |
Output is correct |