#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(m+1);
for(int i=1; i<=m; i++)
k[i].pb(0), k[i].pb(n+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(i==kon.fi&&j==kon.se) break;
if(odw[i][j]) continue;
odw[i][j]=true;
if((c[i-1][j]=='.'||c[i-1][j]=='C') && 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]=='.'||c[i+1][j]=='C') && 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]=='.'||c[i][j-1]=='C') && 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]=='.'||c[i][j+1]=='C') && 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<<" "<<gora<<" "<<dol<<" "<<k[j][gora]<<" "<<k[j][dol]<<"\n";
//continue;
if((j-1==0 || c[i][j-1]=='#') || (j==m || c[i][j+1]=='#') || (i-1==0 || c[i-1][j]=='#') || (i==n || c[i+1][j]=='#')){
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(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(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(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}});
}
/* if(j==m || c[i][j+1]=='#')
if(i-1==0 || c[i-1][j]=='#')
if(i==n || c[i+1][j]=='#')
*/ }
cout<<odl[kon.fi][kon.se]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
380 KB |
Output is correct |
9 |
Correct |
1 ms |
684 KB |
Output is correct |
10 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
6 ms |
1748 KB |
Output is correct |
6 |
Correct |
7 ms |
1748 KB |
Output is correct |
7 |
Correct |
6 ms |
1812 KB |
Output is correct |
8 |
Correct |
4 ms |
1764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
388 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
380 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Incorrect |
2 ms |
636 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
380 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |