이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 500 + 3;
int n, m, sx, sy, fx, fy, d[N][N];
string a[N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
d[i][j] = inf;
if (a[i][j] == 'C'){
sx = i; sy = j;
}
if (a[i][j] == 'F'){
fx = i; fy = j;
}
}
}
queue <int> q;
d[sx][sy] = 0;
q.push(sx * m + sy);
while (!q.empty()){
int x = q.front() / m, y = q.front() % m;
q.pop();
for (int i = 0; i < 4; i++){
int X = x + dx[i], Y = y + dy[i];
if (a[X][Y] == '#') {
continue;
}
if (d[X][Y] != inf) continue;
d[X][Y] = d[x][y] + 1;
q.push(X * m + Y);
}
if (a[x][y - 1] == '#' || a[x][y + 1] == '#' || a[x - 1][y] == '#' || a[x + 1][y] == '#'){
int X = x, Y = y;
while (a[X][Y] != '#'){
X++;
}
X--;
if (d[X][Y] == inf){
d[X][Y] = d[x][y] + 1;
q.push(X * m + Y);
}
while (a[X][Y] != '#'){
X--;
}
X++;
if (d[X][Y] == inf){
d[X][Y] = d[x][y] + 1;
q.push(X * m + Y);
}
X = x, Y = y;
while (a[X][Y] != '#'){
Y++;
}
Y--;
if (d[X][Y] == inf){
d[X][Y] = d[x][y] + 1;
q.push(X * m + Y);
}
while (a[X][Y] != '#'){
Y--;
}
Y++;
if (d[X][Y] == inf){
d[X][Y] = d[x][y] + 1;
q.push(X * m + Y);
}
}
}
if (d[fx][fy] == inf){
cout << "nemoguce";
}else{
cout << d[fx][fy];
}
}
# | 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... |
# | 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... |