답안 #733179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
733179 2023-04-30T07:57:09 Z CyberCraze 로봇 (APIO13_robots) C++17
100 / 100
758 ms 124400 KB
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std;

const int INF=250000;

int n,h,w;
char g[500][500];
pair<int,int> end_pos[500][500][4];
int dp[500][500][9][9];
vector<pair<int,int>> q[INF];

bool inside(int x,int y){
    return (x>=0 && y>=0 && x<h && y<w && g[x][y]!='x');
}

int main(){
    cin>>n>>w>>h;
    memset(dp,0x3f,sizeof(dp));//0x3f is infinity for int
    FOR(i,0,h) FOR(j,0,w){
        cin>>g[i][j];
        if(g[i][j]-'0'>0 && g[i][j]-'0'<10){
            dp[i][j][g[i][j]-'1'][g[i][j]-'1']=0;
        }
    }
    FOR(i,0,h)FOR(j,0,w)if(g[i][j]!='x'){
        FOR(k, 0, 4) {
            pair<int, int> pos = {i, j};
            int d = (k + (g[i][j] == 'A') * 3 + (g[i][j] == 'C')) % 4;  // NESW

            while (true) {
                if (d == 0) {
                    if (inside(pos.first - 1, pos.second)) pos.first--;
                    else break;
                } else if (d == 1) {
                    if (inside(pos.first, pos.second + 1)) pos.second++;
                    else break;
                } else if (d == 2) {
                    if (inside(pos.first + 1, pos.second)) pos.first++;
                    else break;
                } else {
                    if (inside(pos.first, pos.second - 1)) pos.second--;
                    else break;
                }

                if (g[pos.first][pos.second] == 'A') d = (d + 3) % 4;
                if (g[pos.first][pos.second] == 'C') d = (d + 1) % 4;
            }

            end_pos[i][j][k] = pos;
        }
    }
    // Find the minimum no. of pushes to get robot k-l to block (i, j)
    FOR(rng, 0, n) {
        FOR(k, 0, n - rng) {
            int l = k + rng;
            FOR(i, 0, h) FOR(j, 0, w) if (g[i][j] != 'x') {
                FOR(d, k, l) {
                    dp[i][j][k][l] =
                        min(dp[i][j][k][l],
                            dp[i][j][k][d] + dp[i][j][d + 1][l]);
                }
            }
        }

        FOR(k, 0, n - rng) {
            int l = k + rng;

            FOR(i, 0, h) FOR(j, 0, w) if (g[i][j] != 'x') {
                if (dp[i][j][k][l] <= INF) q[dp[i][j][k][l]].push_back({i, j});
            }
            
            FOR(i, 0, INF) {
                for (pair<int, int> pos : q[i]) {
                    int x, y;
                    tie(x, y) = pos;
                    if (dp[x][y][k][l] == i) {
                        FOR(d, 0, 4) {
                            int nx, ny;
                            tie(nx, ny) = end_pos[x][y][d];
                            if (dp[nx][ny][k][l] > dp[x][y][k][l] + 1) {
                                q[dp[nx][ny][k][l] = dp[x][y][k][l] + 1].push_back({nx, ny});
                            }
                        }
                    }
                }
                q[i].clear();
            }
        }
    }

    int ans = INT_MAX;
    FOR(i, 0, h) FOR(j, 0, w) ans = min(ans, dp[i][j][0][n - 1]);
    cout << (ans > INF ? -1 : ans);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 85324 KB Output is correct
2 Correct 35 ms 85352 KB Output is correct
3 Correct 33 ms 85452 KB Output is correct
4 Correct 38 ms 85420 KB Output is correct
5 Correct 35 ms 85420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 85324 KB Output is correct
2 Correct 35 ms 85352 KB Output is correct
3 Correct 33 ms 85452 KB Output is correct
4 Correct 38 ms 85420 KB Output is correct
5 Correct 35 ms 85420 KB Output is correct
6 Correct 40 ms 85408 KB Output is correct
7 Correct 36 ms 85336 KB Output is correct
8 Correct 33 ms 85416 KB Output is correct
9 Correct 34 ms 85400 KB Output is correct
10 Correct 36 ms 85500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 85324 KB Output is correct
2 Correct 35 ms 85352 KB Output is correct
3 Correct 33 ms 85452 KB Output is correct
4 Correct 38 ms 85420 KB Output is correct
5 Correct 35 ms 85420 KB Output is correct
6 Correct 40 ms 85408 KB Output is correct
7 Correct 36 ms 85336 KB Output is correct
8 Correct 33 ms 85416 KB Output is correct
9 Correct 34 ms 85400 KB Output is correct
10 Correct 36 ms 85500 KB Output is correct
11 Correct 176 ms 92620 KB Output is correct
12 Correct 47 ms 89252 KB Output is correct
13 Correct 116 ms 89548 KB Output is correct
14 Correct 262 ms 97856 KB Output is correct
15 Correct 150 ms 89956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 85324 KB Output is correct
2 Correct 35 ms 85352 KB Output is correct
3 Correct 33 ms 85452 KB Output is correct
4 Correct 38 ms 85420 KB Output is correct
5 Correct 35 ms 85420 KB Output is correct
6 Correct 40 ms 85408 KB Output is correct
7 Correct 36 ms 85336 KB Output is correct
8 Correct 33 ms 85416 KB Output is correct
9 Correct 34 ms 85400 KB Output is correct
10 Correct 36 ms 85500 KB Output is correct
11 Correct 176 ms 92620 KB Output is correct
12 Correct 47 ms 89252 KB Output is correct
13 Correct 116 ms 89548 KB Output is correct
14 Correct 262 ms 97856 KB Output is correct
15 Correct 150 ms 89956 KB Output is correct
16 Correct 543 ms 93484 KB Output is correct
17 Correct 758 ms 124400 KB Output is correct
18 Correct 310 ms 96024 KB Output is correct
19 Correct 386 ms 93548 KB Output is correct
20 Correct 659 ms 106808 KB Output is correct