답안 #261608

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261608 2020-08-11T22:26:47 Z DS007 로봇 (APIO13_robots) C++14
100 / 100
875 ms 109688 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 9, W = 500;
int dp[N][N][W][W], pt[W][W], nxt[W][W][5];
vector<int> adj[W * W];
bool explored[W][W][5];
string a[W];
int n, w, h;

int dy[] = {0, 1, 0, -1, 0};
int dx[] = {0, 0, 1, 0, -1};

bool check(int i, int j) {
    if (i < 0 || j < 0 || i >= h || j >= w || a[i][j] == 'x')
        return false;
    return true;
}

int dfs(int i, int j, int dir) {
    if (explored[i][j][dir])
        return nxt[i][j][dir];

    explored[i][j][dir] = true;
    nxt[i][j][dir] = pt[i][j];
    if (a[i][j] != 'A' && a[i][j] != 'C' && a[i][j] != 'x') {
        if (check(i + dx[dir], j + dy[dir]))
            return nxt[i][j][dir] = dfs(i + dx[dir], j + dy[dir], dir);
        else
            return nxt[i][j][dir] = pt[i][j];
    }

    if (a[i][j] == 'C') {
        int nd = dir + 1;
        if (nd == 5)
            nd = 1;

        if (check(i + dx[nd], j + dy[nd]))
            return nxt[i][j][dir] = dfs(i + dx[nd], j + dy[nd], nd);
        else
            return nxt[i][j][dir] = pt[i][j];
    }

    if (a[i][j] == 'A') {
        int nd = dir - 1;
        if (nd == 0)
            nd = 4;

        if (check(i + dx[nd], j + dy[nd]))
            return nxt[i][j][dir] = dfs(i + dx[nd], j + dy[nd], nd);
        else
            return nxt[i][j][dir] = pt[i][j];
    }

    return -1;
}

void dijkstra(int i, int j) {
    vector<int> dist[w * h];
    bool explored[w * h] = {};

    for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++)
            if (a[k][l] != 'x' && dp[i][j][k][l] < w * h)
                dist[dp[i][j][k][l]].push_back(pt[k][l]);
    }

    for (int i_ = 0; i_ < w * h; i_++) {
        for (int j_ : dist[i_]) {
            if (explored[j_])
                continue;

            explored[j_] = true;
            for (int k : adj[j_]) {
                if (dp[i][j][k / w][k % w] > i_ + 1)
                    dp[i][j][k / w][k % w] = i_ + 1, dist[i_ + 1].push_back(k);
            }
        }
    }
}

int solveTestCase() {
    cin >> n >> w >> h;
    for (int i = 0; i < h; i++) cin >> a[i];

    for (int i = 0; i < h; i++){
        for (int j = 0; j < w; j++)
            pt[i][j] = w * i + j;
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (!explored[i][j][1])
                dfs(i, j, 1);
            if (!explored[i][j][2])
                dfs(i, j, 2);
            if (!explored[i][j][3])
                dfs(i, j, 3);
            if (!explored[i][j][4])
                dfs(i, j, 4);

            adj[pt[i][j]].push_back(nxt[i][j][1]);
            adj[pt[i][j]].push_back(nxt[i][j][2]);
            adj[pt[i][j]].push_back(nxt[i][j][3]);
            adj[pt[i][j]].push_back(nxt[i][j][4]);
        }
    }

    //cerr << check(0 + dx[3], 0 + dy[3]) << "\n";
    //cerr << nxt[0][0][1] << " " << nxt[0][0][2] << " " << nxt[0][0][3] << " " << nxt[0][0][4] << "\n";

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < h; k++) {
                for (int l = 0; l < w; l++)
                dp[i][j][k][l] = 1e9;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int k = 0; k < h; k++) {
            for (int l = 0; l < w; l++) {
                if (a[k][l] - '0' == i + 1)
                    dp[i][i][k][l] = 0;
            }
        }

        dijkstra(i, i);
    }

    //for (int k = 0; k < h; k++) {
    //    for (int l = 0; l < w; l++)
    //        cerr << dp[0][0][k][l] << " ";
    //    cerr << "\n";
    //}

    for (int dif = 1; dif < n; dif++) {
        for (int i = 0; i < n; i++) {
            int j = i + dif;
            if (j >= n)
                continue;

            for (int x = i; x < j; x++) {
                for (int k = 0; k < h; k++) {
                    for (int l = 0; l < w; l++)
                        dp[i][j][k][l] = min(dp[i][j][k][l], dp[i][x][k][l] + dp[x + 1][j][k][l]);
                }
            }

            dijkstra(i, j);
        }
    }

    int ans = 1e9;
    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 == 1e9 ? -1 : ans);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    //cin >> t;
    while (t--)
        solveTestCase();
}


Compilation message

robots.cpp: In function 'int solveTestCase()':
robots.cpp:162:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6272 KB Output is correct
2 Correct 4 ms 6272 KB Output is correct
3 Correct 4 ms 6272 KB Output is correct
4 Correct 5 ms 6400 KB Output is correct
5 Correct 5 ms 6400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6272 KB Output is correct
2 Correct 4 ms 6272 KB Output is correct
3 Correct 4 ms 6272 KB Output is correct
4 Correct 5 ms 6400 KB Output is correct
5 Correct 5 ms 6400 KB Output is correct
6 Correct 5 ms 6272 KB Output is correct
7 Correct 5 ms 6272 KB Output is correct
8 Correct 5 ms 6272 KB Output is correct
9 Correct 4 ms 6272 KB Output is correct
10 Correct 5 ms 6400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6272 KB Output is correct
2 Correct 4 ms 6272 KB Output is correct
3 Correct 4 ms 6272 KB Output is correct
4 Correct 5 ms 6400 KB Output is correct
5 Correct 5 ms 6400 KB Output is correct
6 Correct 5 ms 6272 KB Output is correct
7 Correct 5 ms 6272 KB Output is correct
8 Correct 5 ms 6272 KB Output is correct
9 Correct 4 ms 6272 KB Output is correct
10 Correct 5 ms 6400 KB Output is correct
11 Correct 155 ms 63776 KB Output is correct
12 Correct 30 ms 16632 KB Output is correct
13 Correct 72 ms 44792 KB Output is correct
14 Correct 347 ms 64120 KB Output is correct
15 Correct 134 ms 63608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6272 KB Output is correct
2 Correct 4 ms 6272 KB Output is correct
3 Correct 4 ms 6272 KB Output is correct
4 Correct 5 ms 6400 KB Output is correct
5 Correct 5 ms 6400 KB Output is correct
6 Correct 5 ms 6272 KB Output is correct
7 Correct 5 ms 6272 KB Output is correct
8 Correct 5 ms 6272 KB Output is correct
9 Correct 4 ms 6272 KB Output is correct
10 Correct 5 ms 6400 KB Output is correct
11 Correct 155 ms 63776 KB Output is correct
12 Correct 30 ms 16632 KB Output is correct
13 Correct 72 ms 44792 KB Output is correct
14 Correct 347 ms 64120 KB Output is correct
15 Correct 134 ms 63608 KB Output is correct
16 Correct 234 ms 106988 KB Output is correct
17 Correct 875 ms 109688 KB Output is correct
18 Correct 294 ms 108516 KB Output is correct
19 Correct 228 ms 107000 KB Output is correct
20 Correct 567 ms 108760 KB Output is correct