#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 1001;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int r,c;
#define fi first
#define se second
string s[maxN];
ll d[maxN][maxN];
bool vis[maxN][maxN];
#define pli pair<int,int>
#define plii pair<ll,pli>
pli ed;
pli nxt[4][maxN][maxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> r >> c;
priority_queue<plii,vector<plii>,greater<plii>>pq;
for(int i=0;i<r;i++)
{
cin >> s[i];
for(int j=0;j<c;j++)
{
d[i][j]=r*c+10;
vis[i][j]=false;
if(s[i][j]=='S')
{
pq.push({0,{i,j}});
d[i][j]=0;
}
else if(s[i][j]=='C')
{
ed={i,j};
}
}
}
for(int i=0;i<r;i++)
{
int l=0;
for(int j=0;j<c;j++)
{
nxt[0][i][j]={i,l};
if(s[i][j]=='#') l=j+1;
}
l=c-1;
for(int j=c-1;j>=0;j--)
{
nxt[1][i][j]={i,l};
if(s[i][j]=='#') l=j-1;
}
}
for(int j=0;j<c;j++)
{
int u=0;
for(int i=0;i<r;i++)
{
nxt[2][i][j]={u,j};
if(s[i][j]=='#') u=i+1;
}
u=r-1;
for(int i=r-1;i>=0;i--)
{
nxt[3][i][j]={u,j};
if(s[i][j]=='#') u=i-1;
}
}
while(!pq.empty())
{
auto v=pq.top();
pq.pop();
int x=v.se.fi;
int y=v.se.se;
if(vis[x][y]) continue;
if(s[x][y]=='#') continue;
vis[x][y]=true;
for(int i=0;i<4;i++)
{
int newx=x+dx[i];
int newy=y+dy[i];
if(newx<0||newy<0||newx>=r||newy>=c) continue;
if(s[newx][newy]!='#') if(d[newx][newy]>d[x][y]+1) d[newx][newy]=min(d[newx][newy],d[x][y]+1),pq.push({d[newx][newy],{newx,newy}});
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(i==j) continue;
int w=abs(nxt[j][x][y].fi-x)+abs(nxt[j][x][y].se-y)+1;
int newx=nxt[i][x][y].fi;
int newy=nxt[i][x][y].se;
if(d[newx][newy]>d[x][y]+w)
{
d[newx][newy]=d[x][y]+w;
pq.push({d[newx][newy],{newx,newy}});
}
}
}
}
cout << d[ed.fi][ed.se];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
2 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
7 ms |
6100 KB |
Output is correct |
6 |
Correct |
7 ms |
6100 KB |
Output is correct |
7 |
Correct |
8 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
6 ms |
6204 KB |
Output is correct |
15 |
Correct |
7 ms |
6100 KB |
Output is correct |
16 |
Correct |
8 ms |
6220 KB |
Output is correct |
17 |
Correct |
8 ms |
6228 KB |
Output is correct |
18 |
Correct |
10 ms |
6228 KB |
Output is correct |
19 |
Correct |
10 ms |
6264 KB |
Output is correct |
20 |
Correct |
9 ms |
6228 KB |
Output is correct |
21 |
Correct |
7 ms |
6200 KB |
Output is correct |
22 |
Correct |
7 ms |
6100 KB |
Output is correct |
23 |
Correct |
7 ms |
6100 KB |
Output is correct |
24 |
Correct |
8 ms |
6100 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
1492 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
6 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
7 ms |
6204 KB |
Output is correct |
15 |
Correct |
7 ms |
6208 KB |
Output is correct |
16 |
Correct |
8 ms |
6220 KB |
Output is correct |
17 |
Correct |
10 ms |
6356 KB |
Output is correct |
18 |
Correct |
10 ms |
6228 KB |
Output is correct |
19 |
Correct |
10 ms |
6228 KB |
Output is correct |
20 |
Correct |
9 ms |
6212 KB |
Output is correct |
21 |
Correct |
6 ms |
6196 KB |
Output is correct |
22 |
Correct |
7 ms |
6108 KB |
Output is correct |
23 |
Correct |
9 ms |
6112 KB |
Output is correct |
24 |
Correct |
244 ms |
41796 KB |
Output is correct |
25 |
Correct |
318 ms |
41912 KB |
Output is correct |
26 |
Correct |
246 ms |
41804 KB |
Output is correct |
27 |
Correct |
248 ms |
42008 KB |
Output is correct |
28 |
Correct |
128 ms |
41584 KB |
Output is correct |
29 |
Correct |
159 ms |
41580 KB |
Output is correct |
30 |
Correct |
173 ms |
41640 KB |
Output is correct |
31 |
Correct |
8 ms |
6104 KB |
Output is correct |
32 |
Correct |
227 ms |
41700 KB |
Output is correct |
33 |
Correct |
0 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
1492 KB |
Output is correct |
35 |
Correct |
187 ms |
41640 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
5 ms |
6104 KB |
Output is correct |
38 |
Correct |
98 ms |
41564 KB |
Output is correct |
39 |
Correct |
125 ms |
41592 KB |
Output is correct |