#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define ll long long
const int MAX=1e3+10, MAXV=1e9+10;
int n, m, odl[MAX][MAX];
char c[MAX][MAX];
bool odw[MAX][MAX];
vector<int>w[MAX], k[MAX];
pair<int,int>kon, start;
priority_queue<pair<int,pair<int,int>>>kol;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>c[i][j];
if(c[i][j]=='S')
kol.push({0, {i, j}}), start={i, j};
if(c[i][j]=='#')
w[i].pb(j), k[j].pb(i);
if(c[i][j]=='C')
kon={i, j};
}
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
odl[i][j]=MAXV;
odl[start.fi][start.se]=0;
for(int i=1; i<=n; i++)
w[i].pb(0), w[i].pb(n+1);
for(int i=1; i<=m; i++)
k[i].pb(0), k[i].pb(m+1);
for(int i=1; i<=n; i++)
sort(w[i].begin(), w[i].end());
for(int i=1; i<=m; i++)
sort(k[i].begin(), k[i].end());
while(kol.size()){
auto i=kol.top().se.fi, j=kol.top().se.se;
//cout<<i<<" "<<j<<" "<<odl[i][j]<<"\n";
kol.pop();
if(odw[i][j]) continue;
odw[i][j]=true;
if(c[i-1][j]=='.' && odl[i-1][j]>odl[i][j]+1) odl[i-1][j]=odl[i][j]+1, kol.push({-odl[i-1][j], {i-1, j}});
if(c[i+1][j]=='.' && odl[i+1][j]>odl[i][j]+1) odl[i+1][j]=odl[i][j]+1, kol.push({-odl[i+1][j], {i+1, j}});
if(c[i][j-1]=='.' && odl[i][j-1]>odl[i][j]+1) odl[i][j-1]=odl[i][j]+1, kol.push({-odl[i][j-1], {i, j-1}});
if(c[i][j+1]=='.' && odl[i][j+1]>odl[i][j]+1) odl[i][j+1]=odl[i][j]+1, kol.push({-odl[i][j+1], {i, j+1}});
int prawo=lower_bound(w[i].begin(), w[i].end(), j)-w[i].begin();
int lewo=prawo-1;
int dol=lower_bound(k[j].begin(), k[j].end(), i)-k[j].begin();
int gora=dol-1;
//cout<<i<<" "<<j<<" "<<lewo<<" "<<prawo<<" "<<w[i][lewo]<<" "<<w[i][prawo]<<"\n";
if(j-1==0 || c[i][j-1]=='#')//tel w prawo
if(odl[i][w[i][prawo]-1]>odl[i][j]+1)
odl[i][w[i][prawo]-1]=odl[i][j]+1, kol.push({-odl[i][w[i][prawo]-1], {i, w[i][prawo]-1}});
if(j==m || c[i][j+1]=='#')//tel w lewo
if(odl[i][w[i][lewo]+1]>odl[i][j]+1)
odl[i][w[i][lewo]+1]=odl[i][j]+1, kol.push({-odl[i][w[i][lewo]+1], {i, w[i][lewo]+1}});
if(i-1==0 || c[i-1][j]=='#')//tel w dol
if(odl[k[j][dol]-1][j]>odl[i][j]+1)
odl[k[j][dol]-1][j]=odl[i][j]+1, kol.push({-odl[k[j][dol]-1][j], {k[j][dol]-1, j}});
if(i==n || c[i+1][j]=='#')//tel w gore
if(odl[k[j][gora]+1][j]>odl[i][j]+1)
odl[k[j][gora]+1][j]=odl[i][j]+1, kol.push({-odl[k[j][gora]+1][j], {k[j][gora]+1, j}});
}
cout<<odl[kon.fi][kon.se]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |