Submission #482745

# Submission time Handle Problem Language Result Execution time Memory
482745 2021-10-26T08:04:51 Z JerryLiu06 Robots (APIO13_robots) C++17
100 / 100
1083 ms 146240 KB
// https://oj.uz/problem/view/APIO13_robots
 
// Solution Notes: https://bits-and-bytes.me/2020/06/25/APIO-2013-Robots/
 
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using db = double;
using str = string;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
 
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
 
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(0);
 
#define mp make_pair
#define f first
#define s second
 
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert 
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
 
#define lb lower_bound
#define ub upper_bound
 
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
 
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
 
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
 
const int MOD = 1e9 + 7;
const int MX = 510;
const ll INF = 1e18;
 
const int DIR[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
 
int N, W, H; char G[MX][MX]; int DP[MX][MX][10][10]; 
 
pi pos[MX][MX][4]; vpi dists[MX * MX];
 
bool valid(int i, int j) {
    return !(i < 0 || i >= H || j < 0 || j >= W || G[i][j] == 'x');
}
pi getPos(int i, int j, int dir) {
    if (G[i][j] == 'A') {
        dir = (dir + 1) % 4;
    }
    if (G[i][j] == 'C') {
        dir = (dir + 3) % 4;
    }
    if (!valid(i + DIR[dir][0], j + DIR[dir][1])) {
        return {i, j};
    }
    return getPos(i + DIR[dir][0], j + DIR[dir][1], dir);
}
 
int main() {
    FASTIO;
 
    cin >> N >> W >> H;
    
    F0R(i, H) {
        F0R(j, W) {
            FOR(k, 1, N + 1) {
                FOR(l, 1, N + 1) {
                    DP[i][j][k][l] = MOD;
                }
            }
        }
    }
    F0R(i, H) {
        F0R(j, W) {
            cin >> G[i][j];
 
            if ('1' <= G[i][j] && G[i][j] <= '9') {
                int curRob = G[i][j] - '0';
 
                DP[i][j][curRob][curRob] = 0;
            }
        }
    }
    F0R(i, H) {
        F0R(j, W) {
            F0R(k, 4) {
                pos[i][j][k] = getPos(i, j, k);
            }
            //cout << "{" << pos[i][j][0].f << ", " << pos[i][j][0].s << "}" << " ";
        }
        //cout << "\n";
    }
    F0R(LEN, N) {
        FOR(L, 1, N + 1 - LEN) {
            int R = L + LEN;
 
            FOR(MID, L, R) {
                F0R(i, H) {
                    F0R(j, W) {
                        if (DP[i][j][L][MID] != MOD && DP[i][j][MID + 1][R] != MOD) {
                            ckmin(DP[i][j][L][R], DP[i][j][L][MID] + DP[i][j][MID + 1][R]);
                        }
                    }
                }
            }
            // Dijkstra too slow :( - use large queue to store dists instead
 
            F0R(i, H * W + 1) {
                dists[i].clear();
            }
            F0R(i, H) {
                F0R(j, W) {
                    if (DP[i][j][L][R] != MOD) {
                        dists[DP[i][j][L][R]].pb({i, j});
                    }
                }
            }
            F0R(i, H * W + 1) {
                while (sz(dists[i])) {
                    pi cur = dists[i].back();
                    dists[i].pop_back();
 
                    if (DP[cur.f][cur.s][L][R] < i) {
                        continue;
                    }
                    F0R(dir, 4) {
                        int nX = pos[cur.f][cur.s][dir].f;
                        int nY = pos[cur.f][cur.s][dir].s;
 
                        if (DP[nX][nY][L][R] > i + 1) {
                            dists[i + 1].pb({nX, nY});
                            DP[nX][nY][L][R] = i + 1;
                        }
                    }
                }
            }
        }
    }
    int ans = MOD;
 
    F0R(i, H) {
        F0R(j, W) {
            ckmin(ans, DP[i][j][1][N]);
        }
    }
    cout << ((ans == MOD) ? -1 : ans) << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6348 KB Output is correct
2 Correct 3 ms 6348 KB Output is correct
3 Correct 3 ms 6348 KB Output is correct
4 Correct 3 ms 6476 KB Output is correct
5 Correct 3 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6348 KB Output is correct
2 Correct 3 ms 6348 KB Output is correct
3 Correct 3 ms 6348 KB Output is correct
4 Correct 3 ms 6476 KB Output is correct
5 Correct 3 ms 6476 KB Output is correct
6 Correct 3 ms 6348 KB Output is correct
7 Correct 3 ms 6348 KB Output is correct
8 Correct 4 ms 6348 KB Output is correct
9 Correct 3 ms 6348 KB Output is correct
10 Correct 4 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6348 KB Output is correct
2 Correct 3 ms 6348 KB Output is correct
3 Correct 3 ms 6348 KB Output is correct
4 Correct 3 ms 6476 KB Output is correct
5 Correct 3 ms 6476 KB Output is correct
6 Correct 3 ms 6348 KB Output is correct
7 Correct 3 ms 6348 KB Output is correct
8 Correct 4 ms 6348 KB Output is correct
9 Correct 3 ms 6348 KB Output is correct
10 Correct 4 ms 6476 KB Output is correct
11 Correct 199 ms 50220 KB Output is correct
12 Correct 29 ms 46828 KB Output is correct
13 Correct 139 ms 47172 KB Output is correct
14 Correct 325 ms 55444 KB Output is correct
15 Correct 150 ms 47556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6348 KB Output is correct
2 Correct 3 ms 6348 KB Output is correct
3 Correct 3 ms 6348 KB Output is correct
4 Correct 3 ms 6476 KB Output is correct
5 Correct 3 ms 6476 KB Output is correct
6 Correct 3 ms 6348 KB Output is correct
7 Correct 3 ms 6348 KB Output is correct
8 Correct 4 ms 6348 KB Output is correct
9 Correct 3 ms 6348 KB Output is correct
10 Correct 4 ms 6476 KB Output is correct
11 Correct 199 ms 50220 KB Output is correct
12 Correct 29 ms 46828 KB Output is correct
13 Correct 139 ms 47172 KB Output is correct
14 Correct 325 ms 55444 KB Output is correct
15 Correct 150 ms 47556 KB Output is correct
16 Correct 680 ms 114752 KB Output is correct
17 Correct 1083 ms 146240 KB Output is correct
18 Correct 528 ms 118072 KB Output is correct
19 Correct 580 ms 115008 KB Output is correct
20 Correct 779 ms 128960 KB Output is correct