#include <bits/stdc++.h>
using namespace std;
#define ll long long
char arr[1003][1003];
bool been[1003][1003];
priority_queue<pair<int,pair<int,int> > > pq;
int dirx[4]={1,0,-1,0};
int diry[4]={0,1,0,-1};
int l[1003][1003],r[1003][1003],u[1003][1003],d[1003][1003];
int n,m;
bool check(int x,int y){
if(x<1 || x>n || y<1 || y>m)
return false;
if(been[x][y]==true)
return false;
if(arr[x][y]=='#')
return false;
return true;
}
void input(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>arr[i][j];
if(arr[i][j]=='S'){
pq.push({0,{i,j}});
}
}
}
}
void portals(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(arr[i][j]=='#'){
l[i][j]=j+1;
}
else if(j==1){
l[i][j]=1;
}
else{
l[i][j]=l[i][j-1];
}
}
}
for(int i=1; i<=n; i++){
for(int j=m; j>=1; j--){
if(arr[i][j] == '#'){
r[i][j] = j-1;
}
else if(j == m){
r[i][j] = m;
}
else{
r[i][j] = r[i][j+1];
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(arr[i][j]=='#'){
u[i][j]=i+1;
}
else if(i==1){
u[i][j]=1;
}
else{
u[i][j]=u[i-1][j];
}
}
}
for(int i=n;i>=1;i--){
for(int j=1;j<=m;j++){
if(arr[i][j]=='#'){
d[i][j]=i-1;
}
else if(i==n){
d[i][j]=n;
}
else{
d[i][j]=d[i+1][j];
}
}
}
}
int main(){
input();
portals();
while(!pq.empty()){
int w=-pq.top().first;
int x=pq.top().second.first;
int y=pq.top().second.second;
pq.pop();
if(been[x][y]==true)
continue;
if(arr[x][y]=='C'){
cout<<w<<endl;
return 0;
}
been[x][y]=true;
for(int i=0;i<4;i++){
int nx=x+dirx[i];
int ny=y+diry[i];
if(check(nx,ny)==true){
pq.push({-(w+1),{nx,ny}});
}
}
int ds=min({abs(l[x][y]-y),abs(r[x][y]-y),abs(u[x][y]-x),abs(d[x][y]-x)});
pq.push({-(ds+w+1), {x, l[x][y]} });
pq.push({-(ds+w+1), {x, r[x][y]} });
pq.push({-(ds+w+1), {u[x][y], y} });
pq.push({-(ds+w+1), {d[x][y], y} });
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
1480 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
15 ms |
4188 KB |
Output is correct |
6 |
Correct |
15 ms |
3932 KB |
Output is correct |
7 |
Correct |
12 ms |
4184 KB |
Output is correct |
8 |
Correct |
6 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
2 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
13 ms |
3932 KB |
Output is correct |
15 |
Correct |
16 ms |
3928 KB |
Output is correct |
16 |
Correct |
12 ms |
3932 KB |
Output is correct |
17 |
Correct |
15 ms |
3928 KB |
Output is correct |
18 |
Correct |
18 ms |
4184 KB |
Output is correct |
19 |
Correct |
4 ms |
3928 KB |
Output is correct |
20 |
Correct |
11 ms |
5588 KB |
Output is correct |
21 |
Correct |
12 ms |
3932 KB |
Output is correct |
22 |
Correct |
13 ms |
3928 KB |
Output is correct |
23 |
Correct |
15 ms |
3928 KB |
Output is correct |
24 |
Correct |
33 ms |
4900 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
1372 KB |
Output is correct |
10 |
Correct |
2 ms |
1368 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1368 KB |
Output is correct |
14 |
Correct |
13 ms |
3932 KB |
Output is correct |
15 |
Correct |
16 ms |
4064 KB |
Output is correct |
16 |
Correct |
12 ms |
3932 KB |
Output is correct |
17 |
Correct |
15 ms |
3904 KB |
Output is correct |
18 |
Correct |
21 ms |
4232 KB |
Output is correct |
19 |
Correct |
3 ms |
3928 KB |
Output is correct |
20 |
Correct |
10 ms |
5548 KB |
Output is correct |
21 |
Correct |
12 ms |
3904 KB |
Output is correct |
22 |
Correct |
13 ms |
3876 KB |
Output is correct |
23 |
Correct |
15 ms |
3932 KB |
Output is correct |
24 |
Correct |
532 ms |
19444 KB |
Output is correct |
25 |
Correct |
732 ms |
68232 KB |
Output is correct |
26 |
Correct |
61 ms |
24520 KB |
Output is correct |
27 |
Correct |
212 ms |
43432 KB |
Output is correct |
28 |
Correct |
280 ms |
19100 KB |
Output is correct |
29 |
Correct |
342 ms |
19028 KB |
Output is correct |
30 |
Correct |
308 ms |
19128 KB |
Output is correct |
31 |
Correct |
32 ms |
4948 KB |
Output is correct |
32 |
Execution timed out |
1029 ms |
31192 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |