/*
Trying to solve 1 month I got full score
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 5;
#define ff first
#define ss second
#define pii pair <int, int>
#define pb push_back
char a[N][N];
queue <pii> q;
// yokary saga asak cepe
int aa[] = {-1, 0, 1, 0};
int bb[] = {0, 1, 0, -1};
int n, h, w, state[N][N][4], dp[N][N][10][10];
bool vis[N][N][10];
pii val[N][N][4];
pii ugur (int x, int y, int typ) {
if (a[x][y] == 'A') typ = (typ + 3) % 4;
else if (a[x][y] == 'C') typ = (typ + 1) % 4;
if (state[x][y][typ] == 2) return val[x][y][typ];
else if (state[x][y][typ] == 1) return {-1, -1};
state[x][y][typ] = 1;
int new_x = x + aa[typ], new_y = y + bb[typ];
if (new_x < 1 or new_y < 1 or new_x > h or new_y > w or a[new_x][new_y] == 'x') val[x][y][typ] = {x, y};
else val[x][y][typ] = ugur (new_x, new_y, typ);
state[x][y][typ] = 2;
return val[x][y][typ];
}
int main () {
ios::sync_with_stdio(0);cin.tie(0);
// freopen ("input.txt", "r", stdin);
cin >> n >> w >> h;
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
for (int l = 1; l <= h; ++l) {
for (int r = 1; r <= w; ++r) {
dp[l][r][i][j] = 1e9;
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j + i - 1 <= n; ++j) {
int git = j + i - 1;
vector <vector <pii>> v(N * N + 5);
if (i == 1) {
for (int l = 1; l <= h; ++l) {
for (int r = 1; r <= w; ++r) {
if (int(a[l][r]-48) == j) {
dp[l][r][j][j] = 0;
v[0].pb ({l, r});
}
}
}
}
else {
for (int l = 1; l <= h; ++l) {
for (int r = 1; r <= w; ++r) {
int sym = 1e9;
for (int k = j; k < git; ++k) {
sym = min (sym, dp[l][r][j][k] + dp[l][r][k + 1][git]);
}
dp[l][r][j][git] = sym;
if (sym < N * N) v[sym].pb ({l, r});
}
}
}
// tapmaly dp[l][r][j][git]
for (int k = 0; k < N * N; ++k) {
for (pii x : v[k]) {
for (int l = 0; l < 4; ++l) {
pii new_x = ugur (x.ff, x.ss, l);
if (new_x.ff != -1 and new_x.ss != -1) {
if (dp[new_x.ff][new_x.ss][j][git] > k + 1) {
dp[new_x.ff][new_x.ss][j][git] = k + 1;
v[k + 1].pb ({new_x.ff, new_x.ss});
}
}
}
}
}
}
}
int jogap = 1e9;
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
jogap = min (jogap, dp[i][j][1][n]);
}
}
if (jogap == 1e9) jogap = -1;
cout << jogap;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8448 KB |
Output is correct |
2 |
Correct |
4 ms |
8448 KB |
Output is correct |
3 |
Correct |
6 ms |
8528 KB |
Output is correct |
4 |
Correct |
6 ms |
8536 KB |
Output is correct |
5 |
Correct |
5 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8448 KB |
Output is correct |
2 |
Correct |
4 ms |
8448 KB |
Output is correct |
3 |
Correct |
6 ms |
8528 KB |
Output is correct |
4 |
Correct |
6 ms |
8536 KB |
Output is correct |
5 |
Correct |
5 ms |
8536 KB |
Output is correct |
6 |
Correct |
4 ms |
8448 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8540 KB |
Output is correct |
9 |
Correct |
4 ms |
8448 KB |
Output is correct |
10 |
Correct |
5 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8448 KB |
Output is correct |
2 |
Correct |
4 ms |
8448 KB |
Output is correct |
3 |
Correct |
6 ms |
8528 KB |
Output is correct |
4 |
Correct |
6 ms |
8536 KB |
Output is correct |
5 |
Correct |
5 ms |
8536 KB |
Output is correct |
6 |
Correct |
4 ms |
8448 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8540 KB |
Output is correct |
9 |
Correct |
4 ms |
8448 KB |
Output is correct |
10 |
Correct |
5 ms |
8536 KB |
Output is correct |
11 |
Correct |
266 ms |
75528 KB |
Output is correct |
12 |
Correct |
21 ms |
74944 KB |
Output is correct |
13 |
Correct |
99 ms |
74792 KB |
Output is correct |
14 |
Correct |
484 ms |
75748 KB |
Output is correct |
15 |
Correct |
190 ms |
75020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8448 KB |
Output is correct |
2 |
Correct |
4 ms |
8448 KB |
Output is correct |
3 |
Correct |
6 ms |
8528 KB |
Output is correct |
4 |
Correct |
6 ms |
8536 KB |
Output is correct |
5 |
Correct |
5 ms |
8536 KB |
Output is correct |
6 |
Correct |
4 ms |
8448 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8540 KB |
Output is correct |
9 |
Correct |
4 ms |
8448 KB |
Output is correct |
10 |
Correct |
5 ms |
8536 KB |
Output is correct |
11 |
Correct |
266 ms |
75528 KB |
Output is correct |
12 |
Correct |
21 ms |
74944 KB |
Output is correct |
13 |
Correct |
99 ms |
74792 KB |
Output is correct |
14 |
Correct |
484 ms |
75748 KB |
Output is correct |
15 |
Correct |
190 ms |
75020 KB |
Output is correct |
16 |
Correct |
387 ms |
118804 KB |
Output is correct |
17 |
Correct |
1412 ms |
120580 KB |
Output is correct |
18 |
Correct |
530 ms |
118756 KB |
Output is correct |
19 |
Correct |
386 ms |
118356 KB |
Output is correct |
20 |
Correct |
1072 ms |
118924 KB |
Output is correct |