This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
const int MAX = 1001;
const int INF = 1e6 + 1;
int r, c, sx, sy, ex, ey;
int ans = INF;
int maze[MAX][MAX];
bool vis[MAX][MAX];
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int port();
bool good(int x, int y){
return x >= 0 && x < r && y >= 0 && y < c && !maze[x][y];
}
void trav(int x, int y, int s){
if (x == ex && y == ey) {
ans = min(ans, s);
return;
}
if (vis[x][y]) return;
vis[x][y] = true;
for (int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if (good(nx, ny)){
trav(nx, ny, s + 1);
}
while (good(nx, ny)){
nx += dx[i];
ny += dy[i];
}
trav(nx - dx[i], ny - dy[i], s + 1);
}
vis[x][y] = false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> r >> c;
char t;
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
cin >> t;
maze[i][j] = (t == '#' ? 1 : 0);
if (t == 'S') {
sx = i;
sy = j;
}
if (t == 'C'){
ex = i;
ey = j;
}
}
}
trav(sx, sy, 0);
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |