제출 #1165127

#제출 시각아이디문제언어결과실행 시간메모리
1165127Muhammet로봇 (APIO13_robots)C++20
10 / 100
0 ms328 KiB
#include "bits/stdc++.h"

using namespace std;

#define ff first
#define ss second

const int N = 505;

char a[N][N];

int b[2][N][N], n, w, h;

struct ab {
    int x, y, k;
};

pair <int, int> f1(int i, int j, int x1, int y1) {
    while(a[i + x1][j + y1] != 'x' and i + x1 <= h and i + x1 > 0 and j + y1 <= w and j + y1 > 0) {
        i += x1, j += y1;
    }
    return {i, j};
}

void f(int c, int i, int j) {
    queue <ab> q;
    ab abb;
    abb.x = i, abb.y = j, abb.k = 0;
    b[c][i][j] = 0;
    q.push(abb);
    while(!q.empty()) {
        ab z = q.front();
        q.pop();
        if(b[c][z.x][z.y] != z.k) continue;
        pair <int, int> ind = f1(z.x, z.y, 1, 0);
        if(b[c][ind.ff][ind.ss] > z.k + 1){
            abb.x = ind.ff, abb.y = ind.ss, abb.k = z.k+1;
            b[c][ind.ff][ind.ss] = z.k+1;
            q.push(abb);
        }
        ind = f1(z.x, z.y, 0, 1);
        if(b[c][ind.ff][ind.ss] > z.k + 1){
            abb.x = ind.ff, abb.y = ind.ss, abb.k = z.k+1;
            b[c][ind.ff][ind.ss] = z.k+1;
            q.push(abb);
        }
        ind = f1(z.x, z.y, -1, 0);
        if(b[c][ind.ff][ind.ss] > z.k + 1){
            abb.x = ind.ff, abb.y = ind.ss, abb.k = z.k+1;
            b[c][ind.ff][ind.ss] = z.k+1;
            q.push(abb);
        }
        ind = f1(z.x, z.y, 0, -1);
        if(b[c][ind.ff][ind.ss] > z.k + 1){
            abb.x = ind.ff, abb.y = ind.ss, abb.k = z.k+1;
            b[c][ind.ff][ind.ss] = z.k+1;
            q.push(abb);
        }
    }
}

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

    cin >> n >> w >> h;

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

    f(0, ind1.ff, ind1.ss);
    f(1, ind2.ff, ind2.ss);

    int ans = 1e9;
    for(int i = 1; i <= h; i++) {
        for(int j = 1; j <= w; j++) {
            ans = min(ans, b[0][i][j] + b[1][i][j]);
        }
    }

    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...