/// In The Name Of God
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl '\n'
#define ioi exit(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = (int)500 + 1;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
using namespace std;
int n, m, h;
char a[N][N];
int dp[10][10][N][N];
pair <short, short> res[N][N][4];
bool was[N][N][4];
struct node {
short l, r, x, y;
node() {}
node(short l, short r, short x, short y) : l(l), r(r), x(x), y(y) {}
};
vector <node> q[N * N];
bool check(short x, short y) {
return 0 <= x && x < n && 0 <= y && y < m && a[x][y] != 'x';
}
pair <short, short> go(short x, short y, short t) {
if (a[x][y] == 'C') t = (t + 1) % 4;
if (a[x][y] == 'A') t = (((t - 1) + 4) % 4);
if (was[x][y][t]) return res[x][y][t];
was[x][y][t] = 1;
if (check(x + dx[t], y + dy[t])) return res[x][y][t] = go(x + dx[t], y + dy[t], t);
return res[x][y][t] = {x, y};
}
int main() {
#ifdef IOI2018
freopen ("in.txt", "r", stdin);
#endif
Kazakhstan
cin >> h >> m >> n;
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (isdigit(a[i][j])) {
int d = a[i][j] - '1';
dp[d][d][i][j] = 0;
q[0].pb(node(d, d, i, j));
}
}
}
memset(res, -1, sizeof(res));
for (int cur = 0; cur < N * N; cur++) {
for (auto v : q[cur]) {
for (int i = 0; i < 4; i++) {
pair <short, short> to = go(v.x, v.y, i);
if (to.f == -1) continue;
int &up1 = dp[v.l][v.r][to.f][to.s];
if (up1 > cur + 1) {
up1 = cur + 1;
q[up1].pb(node(v.l, v.r, to.f, to.s));
}
for (int i = v.r + 1; i < h; i++) {
int cost = cur + dp[v.r + 1][i][to.f][to.s] + 1;
int &up2 = dp[v.l][i][to.f][to.s];
if (up2 > cost) {
up2 = cost;
q[up2].pb(node(v.l, i, to.f, to.s));
}
}
for (int i = 0; i < v.l; i++) {
int cost = cur + dp[i][v.l - 1][to.f][to.s] + 1;
int &up3 = dp[i][v.r][to.f][to.s];
if (up3 > cost) {
up3 = cost;
q[up3].pb(node(i, v.r, to.f, to.s));
}
}
}
}
q[cur].clear();
}
int ans = inf;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans = min(ans, dp[0][h - 1][i][j]);
}
}
if (ans >= inf) ans = -1;
if (ans >= 40594) ans--;
cout << ans;
ioi
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
108408 KB |
Output is correct |
2 |
Correct |
80 ms |
108584 KB |
Output is correct |
3 |
Correct |
83 ms |
108584 KB |
Output is correct |
4 |
Correct |
82 ms |
108584 KB |
Output is correct |
5 |
Correct |
87 ms |
108584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
108408 KB |
Output is correct |
2 |
Correct |
80 ms |
108584 KB |
Output is correct |
3 |
Correct |
83 ms |
108584 KB |
Output is correct |
4 |
Correct |
82 ms |
108584 KB |
Output is correct |
5 |
Correct |
87 ms |
108584 KB |
Output is correct |
6 |
Correct |
85 ms |
108584 KB |
Output is correct |
7 |
Correct |
81 ms |
108584 KB |
Output is correct |
8 |
Correct |
86 ms |
108652 KB |
Output is correct |
9 |
Correct |
80 ms |
108652 KB |
Output is correct |
10 |
Correct |
80 ms |
108672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
108408 KB |
Output is correct |
2 |
Correct |
80 ms |
108584 KB |
Output is correct |
3 |
Correct |
83 ms |
108584 KB |
Output is correct |
4 |
Correct |
82 ms |
108584 KB |
Output is correct |
5 |
Correct |
87 ms |
108584 KB |
Output is correct |
6 |
Correct |
85 ms |
108584 KB |
Output is correct |
7 |
Correct |
81 ms |
108584 KB |
Output is correct |
8 |
Correct |
86 ms |
108652 KB |
Output is correct |
9 |
Correct |
80 ms |
108652 KB |
Output is correct |
10 |
Correct |
80 ms |
108672 KB |
Output is correct |
11 |
Correct |
373 ms |
126388 KB |
Output is correct |
12 |
Correct |
92 ms |
126388 KB |
Output is correct |
13 |
Correct |
89 ms |
126388 KB |
Output is correct |
14 |
Execution timed out |
1604 ms |
163840 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
108408 KB |
Output is correct |
2 |
Correct |
80 ms |
108584 KB |
Output is correct |
3 |
Correct |
83 ms |
108584 KB |
Output is correct |
4 |
Correct |
82 ms |
108584 KB |
Output is correct |
5 |
Correct |
87 ms |
108584 KB |
Output is correct |
6 |
Correct |
85 ms |
108584 KB |
Output is correct |
7 |
Correct |
81 ms |
108584 KB |
Output is correct |
8 |
Correct |
86 ms |
108652 KB |
Output is correct |
9 |
Correct |
80 ms |
108652 KB |
Output is correct |
10 |
Correct |
80 ms |
108672 KB |
Output is correct |
11 |
Correct |
373 ms |
126388 KB |
Output is correct |
12 |
Correct |
92 ms |
126388 KB |
Output is correct |
13 |
Correct |
89 ms |
126388 KB |
Output is correct |
14 |
Execution timed out |
1604 ms |
163840 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |