Cod sursa(job #2116428)

Utilizator MateiTrandafirMatei Trandafir MateiTrandafir Data 27 ianuarie 2018 16:59:43
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <fstream>
#include <iostream>

#define N 102

struct pos {
    short int x, y;
};

pos q[N * N];
const short int dx[] = {-1, 0, 1, 0};
const short int dy[] = {0, 1, 0, -1};
int i, j, n, m, st, dr;
char a[N][N];
short int ar[N][N], aj[N][N];

inline void lee(pos init, short int a1[N][N]) {
    st = 0; dr = 0;
    q[dr] = init;
    pos p1{}, p2{};
    while (dr >= st) {
        p1 = q[st];
        ++st;
        for (i = 0; i < 4; ++i) {
            p2.x = p1.x + dx[i];
            p2.y = p1.y + dy[i];
            if (a[p2.x][p2.y] == ' ' && a1[p2.x][p2.y] == 0) {
                a1[p2.x][p2.y] = static_cast<char>(a1[p1.x][p1.y] + 1);
                q[++dr] = p2;
            }
        }
    }
}

int main() {
    std::ifstream in("rj.in");
    std::ofstream out("rj.out");
    pos R{}, J{};
    in >> n >> m;
    in.ignore(1);
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m; ++j) {
            a[i][j] = static_cast<char>(in.get());
            if (a[i][j] == 'R') {
                R.x = static_cast<short>(i);
                R.y = static_cast<short>(j);
                ar[i][j] = 0;
                aj[i][j] = 0;
            } else if (a[i][j] == 'J') {
                J.x = static_cast<short>(i);
                J.y = static_cast<short>(j);
            }
        }
        in.ignore(1);
    }
    ++n; ++m;
    for (i = 0; i <= n; ++i) a[i][0] = a[i][m] = 'X';
    for (i = 0; i <= m; ++i) a[0][i] = a[n][i] = 'X';
    --n; --m;
    lee(R, ar);
    lee(J, aj);
    short int t = 5000;
    pos x{};
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m; ++j) {
            if (ar[i][j] == aj[i][j] && ar[i][j] > 0 && ar[i][j] < t) {
                t = ar[i][j];
                x.x = static_cast<short>(i);
                x.y = static_cast<short>(j);
            }
        }
    }
    out << t << ' ' << x.x << ' ' << x.y;
    return 0;
}