답안 #623240

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
623240 2022-08-05T11:07:47 Z jhwest2 로봇 (APIO13_robots) C++17
60 / 100
1500 ms 49392 KB
#include <bits/stdc++.h>
using namespace std;

const int dx[4] = { 0, 1, 0, -1 };
const int dy[4] = { -1, 0, 1, 0 };
const int INF = 0x3f3f3f3f;

int n, w, h, go[1515151], a[1515151];
int dist[505][505], dp[9][9][505][505], g[505][505];
string S[505];

int main() {
    cin.tie(0); ios_base::sync_with_stdio(0);
    cin >> n >> w >> h;
    for (int i = 0; i < h; i++) {
        cin >> S[i];
    }
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        int x = w * i + j;
        int y[4] = {};
        for (int k = 0; k < 4; k++) {
            int ni = i + dx[k];
            int nj = j + dy[k];
            if (ni < 0 || nj < 0 || ni >= h || nj >= w || S[ni][nj] == 'x') {
                y[k] = -1;
            }
            else y[k] = w * ni + nj;
        }
        go[5 * x] = 5 * x;
        for (int k = 0; k < 4; k++) {
            if (S[i][j] == 'A') {
                if (y[(k + 1) % 4] == -1) {
                    go[5 * x + 1 + k] = 5 * x;
                }
                else go[5 * x + 1 + k] = 5 * y[(k + 1) % 4] + 1 + (k + 1) % 4;
            }
            else if (S[i][j] == 'C') {
                if (y[(k + 3) % 4] == -1) {
                    go[5 * x + 1 + k] = 5 * x;
                }
                else go[5 * x + 1 + k] = 5 * y[(k + 3) % 4] + 1 + (k + 3) % 4;
            }
            else if (S[i][j] != 'x') {
                if (y[k] == -1) {
                    go[5 * x + 1 + k] = 5 * x;
                }
                else go[5 * x + 1 + k] = 5 * y[k] + 1 + k;
            }
        }
    }
    for (int k = 0; k < 30; k++) {
        for (int i = 0; i < 5 * w * h; i++) {
            go[i] = go[go[i]];
        }
    }
    
    iota(a, a + w * h, 0);
    for (int d = 0; d < n; d++) for (int i = 0; i < n - d; i++) {
        int j = i + d;
        for (int x = 0; x < h; x++) for (int y = 0; y < w; y++) {
            g[x][y] = INF;
            if (d == 0) {
                g[x][y] = (S[x][y] - '1' == i) ? 0 : INF;
            }
            else {
                for (int k = i; k < j; k++) {
                    g[x][y] = min(g[x][y], dp[i][k][x][y] + dp[k + 1][j][x][y]);
                }
            }
        }
        memset(dist, 0x3f, sizeof dist);
        sort(a, a + w * h, [&](int i, int j) {
            return g[i / w][i % w] < g[j / w][j % w];
            });

        queue<pair<int, int>> Q, R;
        for (int i = 0; i < w * h; i++) {
            if (g[a[i] / w][a[i] % w] != INF) {
                Q.emplace(5 * a[i], g[a[i] / w][a[i] % w]);
            }
            else break;
        }
        while (!Q.empty() || !R.empty()) {
            int v, d;
            if (Q.empty() || (!R.empty() && Q.front().second >= R.front().second)) {
                tie(v, d) = R.front(); R.pop();
            }
            else {
                tie(v, d) = Q.front(); Q.pop();
            }
            // cout << i << ' ' << j << ' ' << "(" << (v / 5 / w) << ", " << (v / 5 % w) << ") " << ' ' << d << '\n';
            assert(v % 5 == 0);
            
            if (dist[v / 5 / w][v / 5 % w] != INF) continue;
            dist[v / 5 / w][v / 5 % w] = d;
            
            for (int k = 1; k <= 4; k++) {
                if (go[v + k] % 5 != 0) continue;
                R.emplace(go[v + k], d + 1);
            }
        }
        for (int x = 0; x < h; x++) for (int y = 0; y < w; y++) {
            dp[i][j][x][y] = dist[x][y];
        }
    }
    int ans = INF;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        ans = min(ans, dp[0][n - 1][i][j]);
    }
    cout << (ans == INF ? -1 : ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 1 ms 1364 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 1 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 1 ms 1364 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 1 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 1 ms 1364 KB Output is correct
8 Correct 1 ms 1364 KB Output is correct
9 Correct 1 ms 1364 KB Output is correct
10 Correct 1 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 1 ms 1364 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 1 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 1 ms 1364 KB Output is correct
8 Correct 1 ms 1364 KB Output is correct
9 Correct 1 ms 1364 KB Output is correct
10 Correct 1 ms 1364 KB Output is correct
11 Correct 628 ms 31140 KB Output is correct
12 Correct 24 ms 4684 KB Output is correct
13 Correct 349 ms 20964 KB Output is correct
14 Correct 757 ms 31268 KB Output is correct
15 Correct 569 ms 31040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 1 ms 1364 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 1 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 1 ms 1364 KB Output is correct
8 Correct 1 ms 1364 KB Output is correct
9 Correct 1 ms 1364 KB Output is correct
10 Correct 1 ms 1364 KB Output is correct
11 Correct 628 ms 31140 KB Output is correct
12 Correct 24 ms 4684 KB Output is correct
13 Correct 349 ms 20964 KB Output is correct
14 Correct 757 ms 31268 KB Output is correct
15 Correct 569 ms 31040 KB Output is correct
16 Execution timed out 1556 ms 49392 KB Time limit exceeded
17 Halted 0 ms 0 KB -