#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define fi first
#define se second
#define INF 1e6
#define unsigned u
#define eps 1e-18
#define eps1 1e-25
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MAX_A 100005
#define V 450
#define re register
#define maxi(a,b) ((a) > (b) ? (a) : (b))
ll MOD = 998244353;
using namespace std;
char grid[1005][1005];
int mov[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
int main(){
optimise;
int r,c;
cin >> r >> c;
ii start, end;
int dis[r][c];
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cin >> grid[i][j];
dis[i][j] = INF;
if(grid[i][j] == 'S'){
start.fi = i,start.se = j;
} else if(grid[i][j] == 'C'){
end.fi = i,end.se = j;
}
}
}
dis[start.fi][start.se] = 0;
queue<ii> q;
q.push(start);
while(!q.empty()){
ii cur = q.front(); q.pop();
for (int i = 0; i < 4; ++i)
{
int nx = cur.fi + mov[i][0], ny = cur.se + mov[i][1];
if(nx < 0 || ny < 0 || nx >= r || ny >= c || grid[nx][ny] == '#')
continue;
if(dis[nx][ny] > dis[cur.fi][cur.se] +1 ){
dis[nx][ny] = dis[cur.fi][cur.se] +1 ;
q.push({nx,ny});
}
}
for (int i = 0; i < 4; ++i)
{
int nx = cur.fi + mov[i][0], ny = cur.se + mov[i][1];
while(nx >= 0 && ny >= 0 && nx < r && ny < c && grid[nx][ny] != '#')
{
nx += mov[i][0], ny += mov[i][1];
}
nx -= mov[i][0], ny -= mov[i][1];
if(nx < 0 || ny < 0 || nx >= r || ny >= c || grid[nx][ny] == '#')
continue;
if(dis[nx][ny] > dis[cur.fi][cur.se] +1 ){
dis[nx][ny] = dis[cur.fi][cur.se] +1 ;
q.push({nx,ny});
}
}
}
cout << dis[end.fi][end.se];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
588 KB |
Output is correct |
6 |
Correct |
4 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
720 KB |
Output is correct |
8 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |