제출 #46899

#제출 시각아이디문제언어결과실행 시간메모리
46899minkank로봇 (APIO13_robots)C++17
60 / 100
1583 ms163840 KiB
#include <bits/stdc++.h> using namespace std; const int N = 505; const int M = 10; const int INF = 1e9; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; struct State { int r, c, x, y, d; }; int n, h, w; int d1[M][M][N][N]; int d2[N][N][5][2]; char a[N][N]; vector<State> vec[N * N]; // A -1 // C +1 void bfs() { for (int i = 0; i <= w * h; ++i) { // cout << "#bfs-at " << i << '\n'; while (vec[i].size()) { State u = vec[i].back(); vec[i].pop_back(); if (u.d != d2[u.r][u.c][u.x][u.y]) continue; // cout << "#bfs-state " << u.r << ' ' << u.c << ' ' << u.x << ' ' << u.y << ' ' << u.d << '\n'; if (u.x == 0) { for (int j = 0; j < 4; ++j) { State v = u; v.r += dr[j], v.c += dc[j], v.x = j + 1, v.y = 0, v.d++; if (v.r < 1 || v.c < 1 || v.r > h || v.c > w || a[v.r][v.c] == 'x') continue; if (d2[v.r][v.c][v.x][v.y] > v.d) { d2[v.r][v.c][v.x][v.y] = v.d, vec[v.d].push_back(v); } } } else { if (u.y == 0 && (a[u.r][u.c] == 'A' || a[u.r][u.c] == 'C')) { if (a[u.r][u.c] == 'A') { State v = u; v.x--, v.y = 1; if (v.x == 0) v.x = 4; if (d2[v.r][v.c][v.x][v.y] > v.d) { d2[v.r][v.c][v.x][v.y] = v.d, vec[v.d].push_back(v); } } else { State v = u; v.x++, v.y = 1; if (v.x == 5) v.x = 1; if (d2[v.r][v.c][v.x][v.y] > v.d) { d2[v.r][v.c][v.x][v.y] = v.d, vec[v.d].push_back(v); } } } else { State v = u; v.r += dr[v.x - 1], v.c += dc[v.x - 1], v.y = 0; if (v.r < 1 || v.c < 1 || v.r > h || v.c > w || a[v.r][v.c] == 'x') { v = u, v.x = v.y = 0; if (d2[v.r][v.c][v.x][v.y] > v.d) { d2[v.r][v.c][v.x][v.y] = v.d, vec[v.d].push_back(v); } } else { if (d2[v.r][v.c][v.x][v.y] > v.d) { d2[v.r][v.c][v.x][v.y] = v.d, vec[v.d].push_back(v); } } } } } } } void go(int l, int r) { for (int i = 0; i <= w * h; ++i) vec[i].clear(); for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { for (int k = 0; k < 5; ++k) { for (int l = 0; l < 2; ++l) { d2[i][j][k][l] = INF; } } d2[i][j][0][0] = d1[l][r][i][j]; if (d2[i][j][0][0] <= w * h) { vec[d2[i][j][0][0]].push_back({i, j, 0, 0, d2[i][j][0][0]}); } } } bfs(); for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { d1[l][r][i][j] = d2[i][j][0][0]; } } } int main() { ios::sync_with_stdio(false); cin >> n >> w >> h; for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { cin >> a[i][j]; } } for (int l = 1; l <= n; ++l) { for (int r = l; r <= n; ++r) { for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { d1[l][r][i][j] = INF; } } } } for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { if (a[i][j] >= '1' && a[i][j] <= '9') { int x = a[i][j] - '0'; d1[x][x][i][j] = 0; } } } for (int i = 1; i <= n; ++i) { go(i, i); // cout << "#at " << i << '\n'; // for (int j = 1; j <= h; ++j) { // for (int k = 1; k <= w; ++k) { // cout << d1[i][i][j][k] << ' '; // } // cout << '\n'; // } } for (int i = 2; i <= n; ++i) { for (int l = 1; l + i - 1 <= n; ++l) { int r = l + i - 1; for (int m = l; m < r; ++m) { for (int j = 1; j <= h; ++j) { for (int k = 1; k <= w; ++k) { d1[l][r][j][k] = min(d1[l][r][j][k], d1[l][m][j][k] + d1[m + 1][r][j][k]); } } } go(l, r); } } int res = INF; for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { res = min(res, d1[1][n][i][j]); } } if (res == INF) cout << -1; else cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...