#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef U_U
#include "algo/debug.h"
#else
#define deb(x...) 42
#endif
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define szof(x) (int)x.size()
#define int ll
#define love bool
template<class T> void MIN(T& a, const T b) { b < a ? a = b, 1 : 0; }
template<class T> void MAX(T& a, const T b) { a < b ? a = b, 1 : 0; }
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int inf = 2147483647;
const int INF = 9223372036854775807;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
// Follow the white rabbit.
int n, w, h;
char e[505][505];
int dist[505][505];
bool check (int i, int j) {
if (i > h || i < 1 || j > w || j < 1 || e[i][j] == 'x') return 0;
return 1;
}
bool second_check (int i, int j) {
if (i == h || i == 1 || j == w || j == 1) return 1;
for (int k = 0; k < 4; k ++) {
int x = i + dx[k], y = j + dy[k];
if (e[x][y] == 'x') return 1;
}
return 0;
}
void ka () {
map <int, int> left, right;
left[0] = 3;
left[1] = 2;
left[2] = 0;
left[3] = 1;
right[0] = 2;
right[1] = 3;
right[2] = 1;
right[3] = 0;
cin >> n >> w >> h;
vector <pair <int, int> > v;
for (int i = 1; i <= h; i ++) {
for (int j = 1; j <= w; j ++) {
cin >> e[i][j];
if (e[i][j] >= '1' && e[i][j] <= '9') v.pb({i, j});
}
}
int ans = inf;
for (int i = 1; i <= h; i ++) {
for (int j = 1; j <= w; j ++) {
if (!second_check(i, j)) continue;
int lol = 0;
for (pair <int, int> c : v) {
queue <pair <int, int> > q;
q.push(c);
for (int I = 1; I <= h; I ++) for (int J = 1; J <= w; J ++) dist[I][J] = inf;
dist[c.first][c.second] = 0;
while (szof(q)) {
pair <int, int> tmp = q.front(); q.pop();
for (int k = 0; k < 4; k ++) {
int x = tmp.first, y = tmp.second;
int cur = k;
while (1) {
if (e[x][y] == 'A') cur = left[cur];
if (e[x][y] == 'C') cur = right[cur];
int X = x + dx[cur], Y = y + dy[cur];
if (!check(X, Y)) {
if (dist[x][y] > dist[tmp.first][tmp.second] + 1) {
dist[x][y] = dist[tmp.first][tmp.second] + 1;
q.push({x, y});
}
break;
}
x = X, y = Y;
}
}
}
lol += dist[i][j];
if (lol > ans) break;
}
MIN(ans, lol);
}
}
cout << (ans == inf ? -1 : ans);
}
const bool overflow = 0;
main() {
//freopen("haircut.in" , "r", stdin); freopen("haircut.out", "w", stdout);
cout.setf(ios::fixed);cout.precision(12);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
if (overflow) cin >> tc;
for (int cs = 1; cs <= tc; cs ++) {
#ifdef U_U
cout << ":D\n";
#endif
ka();
//if (cs != tc) cout << '\n';
}
}
Compilation message
robots.cpp:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
103 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |