Cod sursa(job #2206908)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 24 mai 2018 12:37:39
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("rj.in");
ofstream fout("rj.out");

const int NMAX = 100, N = 10005;

struct st{char rj; int t;} a[NMAX + 5][NMAX + 5];

int n, m, dl[4] = {-1, 0, 1, 0}, dc[4] = {0, 1, 0, -1};

queue< pair<int, int> > q;

bool valid(int i, int j) { return i && j && i <= n && j <= m; }

void read(int l) {
    string s;

    getline(fin, s);

    int c = 1;

    for(string :: iterator it = s.begin(); it < s.end(); it++, c++) {
        if(*it == 'R')
            q.push({l, c}), a[l][c].rj = 'R', a[l][c].t = 1;

        if(*it == 'J')
            q.push({l, c}), a[l][c].rj = 'J', a[l][c].t = 1;

        if(*it == ' ' || *it == 'X')
            a[l][c].t = (*it == ' ') ? 0 : 1;
    }

    while(c <= m) a[l][c].t = 0, c++;
}

int main()
{
    fin >> n >> m;

    fin.get();

    for(int l = 1; l <= n; l++) read(l);

    fin.close();

    while(!q.empty()) {
        for(int i = 0; i < 4; i++) {
            int l = q.front().first, c = q.front().second;

            int x = l + dl[i], y = c + dc[i];

            if(!valid(x, y)) continue;

            if(!a[x][y].t) {
                q.push({x, y});
                a[x][y].rj = a[l][c].rj, a[x][y].t = a[l][c].t + 1;
                continue;
            }

            if(a[l][c].rj == 'R' && a[x][y].rj == 'J') {
                fout << a[l][c].t << " " << x << " " << y;
                return 0;
            }

            if(a[l][c].rj == 'J' && a[x][y].rj == 'R') {
                fout << a[l][c].t << " " << x << " " << y;
                return 0;
            }
        }
        q.pop();
    }
}