Submission #1165421

#TimeUsernameProblemLanguageResultExecution timeMemory
1165421MuhammetRobots (APIO13_robots)C++20
30 / 100
12 ms10312 KiB
#include "bits/stdc++.h"

using namespace std;

#define ff first
#define ss second
#define ll long long

const int N = 505;

char a[N][N];

int mp[N][N], n, w, h;

pair <int, int> dp[N][N][4];

vector <vector <int>> v;

pair <int, int> f(int i, int j, int x) {
    if(dp[i][j][x] != pair <int, int> {0, 0}) return dp[i][j][x];
    int x1 = (x % 2 ? 0 : (!x ? -1 : 1)), y1 = (!(x % 2) ? 0 : (x == 1 ? 1 : -1));
    if(a[i + x1][j + y1] != 'x' and i + x1 <= h and i + x1 > 0 and j + y1 <= w and j + y1 > 0) {
        int xx = x, i1 = i, j1 = j;
        i += x1, j += y1;
        if(a[i][j] == 'C') {
            x++;
            if(x == 4) x = 0;
        }
        if(a[i][j] == 'A') {
            x--;
            if(x == -1) x = 3;
        }
        return dp[i1][j1][xx] = f(i, j, x);
    }
    return dp[i][j][x] = pair <int, int> {i, j};
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    cin >> n >> w >> h;

    int nd = 0;
    int ind1, ind2;
    for(int i = 1; i <= h; i++) {
        for(int j = 1; j <= w; j++) {
            cin >> a[i][j];
            mp[i][j] = ++nd;
            if(a[i][j] == '1') ind1 = nd;
            if(a[i][j] == '2') ind2 = nd;
        }
    }

    v.resize(nd+1);
    for(int i = 1; i <= h; i++) {
        for(int j = 1; j <= w; j++) {
            pair <int, int> ind = f(i, j, 0);
            if(ind != pair <int, int> {i, j}) v[mp[i][j]].push_back(mp[ind.ff][ind.ss]);
            ind = f(i, j, 1);
            if(ind != pair <int, int> {i, j}) v[mp[i][j]].push_back(mp[ind.ff][ind.ss]);
            ind = f(i, j, 2);
            if(ind != pair <int, int> {i, j}) v[mp[i][j]].push_back(mp[ind.ff][ind.ss]);
            ind = f(i, j, 3);
            if(ind != pair <int, int> {i, j}) v[mp[i][j]].push_back(mp[ind.ff][ind.ss]);
        }
    }

    vector <int> dis1(nd+1, 1e9), dis2(nd+1, 1e9);
    queue <int> q;
    q.push(ind1);
    dis1[ind1] = 0;
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        for(auto i : v[x]) {
            if(dis1[i] == 1e9) {
                dis1[i] = dis1[x] + 1;
                q.push(i);
            }
        }
    }
    q.push(ind2);
    dis2[ind2] = 0;
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        for(auto i : v[x]) {
            if(dis2[i] == 1e9) {
                dis2[i] = dis2[x] + 1;
                q.push(i);
            }
        }
    }

    int ans = 1e9;
    for(int i = 1; i <= nd; i++) {
        ans = min(ans, dis1[i] + dis2[i]);
    }
    cout << (ans == 1e9 ? -1 : ans);

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...