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 <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;
}
# | 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... |