This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |