#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
// yokary saga asak cepe
// 0 1 2 3
const int N = 15;
int aa[] = {-1, 0, 1, 0};
int bb[] = {0, 1, 0, -1};
pair <int, int> a1, b1;
int n, m, k, dis[N][N], dis1[N][N];
bool dur[N][N][4], vis[N][N], vis1[N][N];
char a[N][N];
bool durmalymy (int x, int y) {
if (min (x, y) < 1 or x > n or y > m or a[x][y] == 'x') return 1;
return 0;
}
pair <int, int> ugur (int x, int y, int val) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
for (int k = 0; k < 4; ++k) {
dur[i][j][k] = 0;
}
}
}
// memset (dur, 0, sizeof(dur));
while (1) {
if (a[x][y] == 'A') {
val--;
}
else if (a[x][y] == 'C') {
val++;
}
if (dur[x][y][val]) {
x = y = 0;
break;
}
val += 4;val %= 4;
dur[x][y][val] = 1;
// cout << "tr : ->> " << x << " " << y << " " << val << " " << x + aa[val] << " " << y + bb[val] << "\n";
if (durmalymy (x + aa[val], y + bb[val])) break;
x += aa[val];y += bb[val];
}
// cout << x
return {x, y};
}
int main () {
// freopen ("input.txt", "r", stdin);
cin >> k >> m >> n;
assert (k == 2);
// cout << "sal ";
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
if (a[i][j] == '1') {
a1 = {i, j};
}
else if (a[i][j] == '2') {
b1 = {i, j};
}
dis[i][j] = dis1[i][j] = 1e9;
}
}
// auto x = ugur (4, 1, 0);
// cout << x.ff << " " << x.ss;
// return 0;
// cout << "sal2 ";
queue <pair <int, int>> q;
q.push({a1.ff, a1.ss});
dis[a1.ff][a1.ss] = 0;
vis[0][0] = vis[a1.ff][a1.ss] = 1;
while (!q.empty()) {
auto op = q.front();
q.pop();
// cout << ": " << op.ff << " " << op.ss << "\n";
for (int i = 0; i < 4; ++i) {
auto x = ugur (op.ff, op.ss, i);
// cout << "dur : " << x.ff << " " << x.ss << "\n";
if (vis[x.ff][x.ss]) continue;
q.push({x.ff, x.ss});
dis[x.ff][x.ss] = dis[op.ff][op.ss] + 1;
vis[x.ff][x.ss] = 1;
}
// return 0;
}
// cout << "sal1 ";
q.push({b1.ff, b1.ss});
dis1[b1.ff][b1.ss] = 0;
vis1[0][0] = vis1[b1.ff][b1.ss] = 1;
// cout << vis1[1][]
while (!q.empty()) {
auto op = q.front();
q.pop();
// cout << ": " << op.ff << " " << op.ss << "\n";
for (int i = 0; i < 4; ++i) {
auto x = ugur (op.ff, op.ss, i);
// cout << i << " " << "dur : -> " << x.ff << " " << x.ss << " ";
if (vis1[x.ff][x.ss]) {
continue;
}
// cout << "\n";
q.push({x.ff, x.ss});
dis1[x.ff][x.ss] = dis1[op.ff][op.ss] + 1;
vis1[x.ff][x.ss] = 1;
}
}
// cout << "cyk " << dis[1][1] << " " << dis1[1][1] << "\n";
//
// return 0;
int jog = 1e9;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
jog = min (jog, dis[i][j] + dis1[i][j]);
}
}
if (jog == 1e9) jog = -1;
cout << jog;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |