Cod sursa(job #2634350)

Utilizator gasparrobert95Gaspar Robert Andrei gasparrobert95 Data 10 iulie 2020 17:36:08
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.58 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int n, m, x, y, a, b, minim, mt[105][105], r[105][105], copie[105][105], ox[] = {-1, 0, 1, 0}, oy[] = {0, 1, 0, -1};
queue < pair <int, int> > q;
string s;

void readInput() {
    fin >> n >> m;
    fin.get();
    for (int i = 1; i <= n; ++i) {
        getline(fin, s);
        for (int j = 0; j < m; ++j) {
            if (s[j] == 'X')
                mt[i][j + 1] = -1;
            else if (s[j] == 'R')
                x = i, y = j + 1;
            else if (s[j] == 'J')
                a = i, b = j + 1;
            copie[i][j + 1] = mt[i][j + 1];
        }
    }
    return;
}

void leeRomeo() {
    while(!q.empty() && mt[a][b] == 0) {
        for (int i = 0; i < 4; ++i)
            if (q.front().first >= 1 && q.front().first <= n && q.front().second >= 1 && q.front().second <= m &&
                mt[q.front().first + ox[i]][q.front().second + oy[i]] == 0) {
                mt[q.front().first + ox[i]][q.front().second + oy[i]] += mt[q.front().first][q.front().second] + 1;
                r[q.front().first + ox[i]][q.front().second + oy[i]] = mt[q.front().first + ox[i]][q.front().second + oy[i]];
                q.push(make_pair(q.front().first + ox[i], q.front().second + oy[i]));
            }
        q.pop();
    }
    return;
}

void leeJulieta() {
    while(!q.empty() && mt[x][y] == 0) {
        for (int i = 0; i < 4; ++i)
            if (q.front().first >= 1 && q.front().first <= n && q.front().second >= 1 && q.front().second <= m &&
                mt[q.front().first + ox[i]][q.front().second + oy[i]] == 0) {
                mt[q.front().first + ox[i]][q.front().second + oy[i]] += mt[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first + ox[i], q.front().second + oy[i]));
            }
        q.pop();
    }
    return;
}

int main() {
    readInput();
    q.push(make_pair(x, y));
    leeRomeo();
    minim = mt[a][b] / 2;
    while (!q.empty())
        q.pop();
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            mt[i][j] = copie[i][j];
    q.push(make_pair(a, b));
    leeJulieta();
    fout << minim << " ";
    bool stop = false;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j)
            if (r[i][j] == mt[i][j] && r[i][j] == minim) {
                fout << i << " " << j;
                stop = true;
                break;
            }
        if (stop)
            break;
    }
    return 0;
}