Submission #207659

# Submission time Handle Problem Language Result Execution time Memory
207659 2020-03-08T10:01:34 Z PeppaPig Robots (APIO13_robots) C++14
60 / 100
1033 ms 163844 KB
#include <bits/stdc++.h>

#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 505;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};

struct item {
    int r, c, id;
    item(int r, int c, int id) : r(r), c(c), id(id) {}
};

int n, w, h, mp[10][10], dp[45][N][N];
char A[N][N];
pii nex[4][N][N];
vector<item> process[45];

pii find_next(int r, int c, int dir) {
    if(nex[dir][r][c] == pii(-1, -1) && A[r][c] != 'x') {
        int ndir = dir;
        if(A[r][c] == 'C') ndir = (dir + 1) % 4;
        if(A[r][c] == 'A') ndir = (dir + 3) % 4;
        int nr = r + dx[ndir], nc = c + dy[ndir];
        if(nr < 1 || nr > h || nc < 1 || nc > w || A[nr][nc] == 'x')
            nex[dir][r][c] = pii(r, c);
        else nex[dir][r][c] = find_next(nr, nc, ndir);
    }
    return nex[dir][r][c];
}

int main() {
    fill_n(nex[0][0], N * N * 4, pii(-1, -1));
    fill_n(dp[0][0], 45 * N * N, 1e9);

    scanf("%d %d %d", &n, &w, &h);
    for(int i = 1; i <= h; i++) scanf(" %s", A[i] + 1);

    for(int i = 1; i <= h; i++) 
        for(int j = 1; j <= w; j++)
            for(int k = 0; k < 4; k++)
                find_next(i, j, k);
    for(int i = 1, ptr = 0; i <= n; i++) for(int j = i; j <= n; j++)
        mp[i][j] = ptr++;

    for(int k = 1; k <= n; k++) {
        queue<item> Q;
        vector<int> candid;
        for(int i = 1; i <= h; i++) for(int j = 1; j <= w; j++) {
            if(A[i][j] == 'x') continue;
            if(k == 1) {
                if('1' <= A[i][j] && A[i][j] <= '9') {
                    int now = mp[A[i][j] - '0'][A[i][j] - '0'];
                    process[now].emplace_back(i, j, now);
                    candid.emplace_back(now);
                    dp[now][i][j] = 0;
                }
            } else {
                for(int x = 1, y = k; y <= n; x++, y++) {
                    int now = mp[x][y];
                    for(int l = x; l < y; l++) {
                        int L = mp[x][l], R = mp[l + 1][y];
                        dp[now][i][j] = min(dp[now][i][j], dp[L][i][j] + dp[R][i][j]);
                    }
                    process[now].emplace_back(i, j, now);
                    candid.emplace_back(now);
                }
            }
        }
        sort(candid.begin(), candid.end());
        candid.resize(unique(candid.begin(), candid.end()) - candid.begin());

        for(int &id : candid) {
            sort(process[id].begin(), process[id].end(), [&](item &a, item &b) {
                return dp[id][a.r][a.c] < dp[id][b.r][b.c];
            });
            for(item &now : process[id]) {
                Q.emplace(now);
                while(!Q.empty()) {
                    item u = Q.front(); Q.pop();
                    for(int i = 0; i < 4; i++) {
                        int nr = u.r + dx[i], nc = u.c + dy[i];
                        if(nr < 1 || nr > h || nc < 1 || nc > w || A[nr][nc] == 'x')
                            continue;
                        pii v = nex[i][nr][nc];
                        if(dp[u.id][u.r][u.c] + 1 < dp[u.id][v.x][v.y]) {
                            dp[u.id][v.x][v.y] = dp[u.id][u.r][u.c] + 1;
                            Q.emplace(v.x, v.y, u.id);
                        }
                    }
                }
            }
        }
        for(int &id : candid) process[id].clear();
    }
    int ans = 1e9;
    for(int i = 1; i <= h; i++) for(int j = 1; j <= w; j++)
        ans = min(ans, dp[mp[1][n]][i][j]); 
    if(ans != 1e9) printf("%d\n", ans);
    else printf("-1\n");

    return 0;
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &w, &h);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:41:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= h; i++) scanf(" %s", A[i] + 1);
                                 ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 34 ms 53240 KB Output is correct
2 Correct 35 ms 53240 KB Output is correct
3 Correct 35 ms 53240 KB Output is correct
4 Correct 36 ms 53240 KB Output is correct
5 Correct 35 ms 53240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 53240 KB Output is correct
2 Correct 35 ms 53240 KB Output is correct
3 Correct 35 ms 53240 KB Output is correct
4 Correct 36 ms 53240 KB Output is correct
5 Correct 35 ms 53240 KB Output is correct
6 Correct 36 ms 53240 KB Output is correct
7 Correct 35 ms 53240 KB Output is correct
8 Correct 36 ms 53240 KB Output is correct
9 Correct 36 ms 53240 KB Output is correct
10 Correct 34 ms 53240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 53240 KB Output is correct
2 Correct 35 ms 53240 KB Output is correct
3 Correct 35 ms 53240 KB Output is correct
4 Correct 36 ms 53240 KB Output is correct
5 Correct 35 ms 53240 KB Output is correct
6 Correct 36 ms 53240 KB Output is correct
7 Correct 35 ms 53240 KB Output is correct
8 Correct 36 ms 53240 KB Output is correct
9 Correct 36 ms 53240 KB Output is correct
10 Correct 34 ms 53240 KB Output is correct
11 Correct 266 ms 77648 KB Output is correct
12 Correct 40 ms 53496 KB Output is correct
13 Correct 150 ms 67844 KB Output is correct
14 Correct 347 ms 77516 KB Output is correct
15 Correct 247 ms 77520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 53240 KB Output is correct
2 Correct 35 ms 53240 KB Output is correct
3 Correct 35 ms 53240 KB Output is correct
4 Correct 36 ms 53240 KB Output is correct
5 Correct 35 ms 53240 KB Output is correct
6 Correct 36 ms 53240 KB Output is correct
7 Correct 35 ms 53240 KB Output is correct
8 Correct 36 ms 53240 KB Output is correct
9 Correct 36 ms 53240 KB Output is correct
10 Correct 34 ms 53240 KB Output is correct
11 Correct 266 ms 77648 KB Output is correct
12 Correct 40 ms 53496 KB Output is correct
13 Correct 150 ms 67844 KB Output is correct
14 Correct 347 ms 77516 KB Output is correct
15 Correct 247 ms 77520 KB Output is correct
16 Runtime error 1033 ms 163844 KB Execution killed with signal 9 (could be triggered by violating memory limits)
17 Halted 0 ms 0 KB -