Cod sursa(job #2116375)

Utilizator MateiTrandafirMatei Trandafir MateiTrandafir Data 27 ianuarie 2018 16:07:50
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 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 st = 0, dr = -1;
char a[N][N];
pos b[N][N];


int main() {
    std::ifstream in("rj.in");
    std::ofstream out("rj.out");
    int i, j, n, m;
    pos J{}, p1{}, p2{};
    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') {
                ++dr;
                q[dr].x = static_cast<short>(i);
                q[dr].y = static_cast<short>(j);
                a[i][j] = 0;
            } else if (a[i][j] == 'J') {
                J.x = static_cast<short>(i);
                J.y = static_cast<short>(j);
                a[i][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;
    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] == ' ') {
                q[++dr] = p2;
                a[p2.x][p2.y] = static_cast<char>(1 + a[p1.x][p1.y]);
                b[p2.x][p2.y] = p1;
            }
        }
    }
    int l = (a[J.x][J.y] + 1) >> 1;
    for (i = 0; i < l; ++i) J = b[J.x][J.y];
    out << l << ' ' << J.x << ' ' << J.y;
    return 0;
}