#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
const ll maxn=1005;
char grid[maxn][maxn];
ll dis[maxn][maxn];
ii wall[maxn][maxn][4]; //N,E,S,W
ll hasWall[maxn][maxn];
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};
vector<ii> v[1000005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll r,c;
ii s,e;
cin>>r>>c;
for (int i=0;i<=r+1;i++){
for (int j=0;j<=c+1;j++){
grid[i][j]='#';
}
}
for (int i=1;i<=r;i++){
for (int j=1;j<=c;j++){
cin>>grid[i][j];
if (grid[i][j]=='S') s={i,j};
if (grid[i][j]=='C') e={i,j};
}
}
memset(dis,63,sizeof(dis));
memset(hasWall,63,sizeof(hasWall));
for (int i=1;i<=r;i++){
ii p={0,0};
ll cnt=0;
for (int j=0;j<=c+1;j++){
if (grid[i][j]=='#')
p={i,j+1},cnt=0;
else{
wall[i][j][0]=p;
cnt++;
hasWall[i][j]=min(hasWall[i][j],cnt);
}
//first open square
}
}
for (int i=1;i<=c;i++){
ii p={0,0};
ll cnt=0;
for (int j=0;j<=r+1;j++){
if (grid[j][i]=='#'){
p={j+1,i},cnt=0;
}
else{
cnt++;
wall[j][i][1]=p;
hasWall[j][i]=min(hasWall[j][i],cnt);
}
}
}
for (int i=1;i<=r;i++){
ii p={0,0};
ll cnt=0;
for (int j=c+1;j>=0;j--){
if (grid[i][j]=='#')
p={i,j-1}, cnt=0;
else{
cnt++;
wall[i][j][2]=p;
hasWall[i][j]=min(hasWall[i][j],cnt);
}
}
}
for (int i=1;i<=c;i++){
ii p={0,0};
ll cnt=0;
for (int j=r+1;j>=0;j--){
if (grid[j][i]=='#'){
p={j-1,i};
cnt=0;
}
else{
cnt++;
wall[j][i][3]=p;
hasWall[j][i]=min(hasWall[j][i],cnt);
}
}
}
v[0].emplace_back(s);
cerr<<1<<endl;
ll d=0;
while (d!=1000005){
ll x,y;
while (v[d].empty()) d++;
tie(x,y)=v[d].back();
//cerr<<d<<' '<<x<<' '<<y<<endl;
v[d].pop_back();
if (dis[x][y]<=d) continue;
dis[x][y]=d;
if (make_pair(x,y)==e) break;
for (int i=0;i<4;i++){
ll nx=wall[x][y][i].first,ny=wall[x][y][i].second;
if (dis[nx][ny]<=d+hasWall[x][y]) continue;
v[d+hasWall[x][y]].emplace_back(nx,ny);
}
for (int i=0;i<4;i++){
ll nx=x+dx[i],ny=y+dy[i];
if (grid[nx][ny]=='#') continue;
if (dis[nx][ny]<=d+1) continue;
v[d+1].emplace_back(nx,ny);
}
}
cout<<dis[e.first][e.second];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
41564 KB |
Output is correct |
2 |
Correct |
9 ms |
41564 KB |
Output is correct |
3 |
Correct |
9 ms |
41680 KB |
Output is correct |
4 |
Correct |
8 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
41564 KB |
Output is correct |
6 |
Correct |
9 ms |
41564 KB |
Output is correct |
7 |
Correct |
8 ms |
41584 KB |
Output is correct |
8 |
Correct |
9 ms |
41560 KB |
Output is correct |
9 |
Correct |
8 ms |
41652 KB |
Output is correct |
10 |
Correct |
8 ms |
41660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41564 KB |
Output is correct |
2 |
Correct |
8 ms |
41604 KB |
Output is correct |
3 |
Correct |
8 ms |
41564 KB |
Output is correct |
4 |
Correct |
8 ms |
41560 KB |
Output is correct |
5 |
Correct |
9 ms |
41560 KB |
Output is correct |
6 |
Correct |
8 ms |
41564 KB |
Output is correct |
7 |
Correct |
9 ms |
41564 KB |
Output is correct |
8 |
Correct |
8 ms |
41564 KB |
Output is correct |
9 |
Correct |
9 ms |
43856 KB |
Output is correct |
10 |
Correct |
9 ms |
43868 KB |
Output is correct |
11 |
Correct |
9 ms |
43704 KB |
Output is correct |
12 |
Correct |
8 ms |
43608 KB |
Output is correct |
13 |
Correct |
9 ms |
43832 KB |
Output is correct |
14 |
Correct |
8 ms |
41564 KB |
Output is correct |
15 |
Correct |
8 ms |
43612 KB |
Output is correct |
16 |
Correct |
8 ms |
41648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
41816 KB |
Output is correct |
2 |
Correct |
8 ms |
41564 KB |
Output is correct |
3 |
Correct |
8 ms |
41564 KB |
Output is correct |
4 |
Correct |
9 ms |
41684 KB |
Output is correct |
5 |
Correct |
15 ms |
54876 KB |
Output is correct |
6 |
Correct |
13 ms |
55132 KB |
Output is correct |
7 |
Correct |
14 ms |
54620 KB |
Output is correct |
8 |
Correct |
14 ms |
55300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41560 KB |
Output is correct |
2 |
Correct |
8 ms |
41564 KB |
Output is correct |
3 |
Correct |
8 ms |
41676 KB |
Output is correct |
4 |
Correct |
9 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
41564 KB |
Output is correct |
6 |
Correct |
9 ms |
41560 KB |
Output is correct |
7 |
Correct |
9 ms |
41692 KB |
Output is correct |
8 |
Correct |
8 ms |
41604 KB |
Output is correct |
9 |
Correct |
9 ms |
43648 KB |
Output is correct |
10 |
Correct |
9 ms |
43864 KB |
Output is correct |
11 |
Correct |
9 ms |
43608 KB |
Output is correct |
12 |
Correct |
9 ms |
43608 KB |
Output is correct |
13 |
Correct |
10 ms |
43700 KB |
Output is correct |
14 |
Correct |
13 ms |
55128 KB |
Output is correct |
15 |
Correct |
13 ms |
55132 KB |
Output is correct |
16 |
Correct |
12 ms |
54860 KB |
Output is correct |
17 |
Correct |
13 ms |
55128 KB |
Output is correct |
18 |
Correct |
13 ms |
55164 KB |
Output is correct |
19 |
Correct |
11 ms |
54108 KB |
Output is correct |
20 |
Correct |
13 ms |
56668 KB |
Output is correct |
21 |
Correct |
14 ms |
54872 KB |
Output is correct |
22 |
Correct |
12 ms |
54872 KB |
Output is correct |
23 |
Correct |
13 ms |
54876 KB |
Output is correct |
24 |
Correct |
12 ms |
55668 KB |
Output is correct |
25 |
Correct |
8 ms |
41604 KB |
Output is correct |
26 |
Correct |
9 ms |
43612 KB |
Output is correct |
27 |
Correct |
9 ms |
41608 KB |
Output is correct |
28 |
Correct |
13 ms |
55132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41564 KB |
Output is correct |
2 |
Correct |
8 ms |
41676 KB |
Output is correct |
3 |
Correct |
8 ms |
41564 KB |
Output is correct |
4 |
Correct |
8 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
41560 KB |
Output is correct |
6 |
Correct |
8 ms |
41564 KB |
Output is correct |
7 |
Correct |
8 ms |
41688 KB |
Output is correct |
8 |
Correct |
8 ms |
41564 KB |
Output is correct |
9 |
Correct |
9 ms |
43636 KB |
Output is correct |
10 |
Correct |
8 ms |
43868 KB |
Output is correct |
11 |
Correct |
9 ms |
43656 KB |
Output is correct |
12 |
Correct |
9 ms |
43612 KB |
Output is correct |
13 |
Correct |
9 ms |
43612 KB |
Output is correct |
14 |
Correct |
13 ms |
54876 KB |
Output is correct |
15 |
Correct |
12 ms |
55132 KB |
Output is correct |
16 |
Correct |
13 ms |
54620 KB |
Output is correct |
17 |
Correct |
12 ms |
55132 KB |
Output is correct |
18 |
Correct |
13 ms |
55128 KB |
Output is correct |
19 |
Correct |
11 ms |
54108 KB |
Output is correct |
20 |
Correct |
12 ms |
56712 KB |
Output is correct |
21 |
Correct |
16 ms |
55132 KB |
Output is correct |
22 |
Correct |
12 ms |
54928 KB |
Output is correct |
23 |
Correct |
13 ms |
54876 KB |
Output is correct |
24 |
Correct |
131 ms |
133360 KB |
Output is correct |
25 |
Correct |
162 ms |
136124 KB |
Output is correct |
26 |
Correct |
67 ms |
115192 KB |
Output is correct |
27 |
Correct |
139 ms |
158212 KB |
Output is correct |
28 |
Correct |
132 ms |
130048 KB |
Output is correct |
29 |
Correct |
121 ms |
130900 KB |
Output is correct |
30 |
Correct |
115 ms |
121424 KB |
Output is correct |
31 |
Correct |
13 ms |
55800 KB |
Output is correct |
32 |
Correct |
165 ms |
139604 KB |
Output is correct |
33 |
Correct |
8 ms |
41560 KB |
Output is correct |
34 |
Correct |
9 ms |
43612 KB |
Output is correct |
35 |
Correct |
121 ms |
117840 KB |
Output is correct |
36 |
Correct |
10 ms |
41820 KB |
Output is correct |
37 |
Correct |
13 ms |
55304 KB |
Output is correct |
38 |
Correct |
111 ms |
136272 KB |
Output is correct |
39 |
Correct |
76 ms |
111444 KB |
Output is correct |