Cod sursa(job #2703524)

Utilizator IoanMihaiIoan Mihai IoanMihai Data 8 februarie 2021 18:32:40
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
const int nmax = 1e2 + 5;

int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};

int n, m, xr, yr, xj, yj, r[nmax][nmax], jl[nmax][nmax];
bool aa[nmax][nmax];
char a[nmax];

void read(){
    fin >> n >> m;
    fin.get();

    for(int i = 1; i <= n; i++){
        fin.getline(a, m + 1);

        for(int j = 0; j < m; j++){
            if(a[j] == 'R')
                xr = i, yr = j + 1;
            else if(a[j] == 'J')
                xj = i, yj = j + 1;

            if(a[j] != 'X')
                aa[i][j + 1] = 1;
        }
    }

}

bool ok(int l, int c){
    return (l && c && l <= n && c <= m && aa[l][c]);
}

void calc(int mt[nmax][nmax], int l, int c){
    mt[l][c] = 1;
    queue <pair<int, int> > q;
    q.push({l, c});

    while(!q.empty()){
        pair<int, int> cell = q.front();
        q.pop();

        for(int i = 0; i < 8; i++){
            int x = cell.first + dx[i];
            int y = cell.second + dy[i];

            if(ok(x, y) && !mt[x][y]){
                mt[x][y] = mt[cell.first][cell.second] + 1;
                q.push({x, y});
            }
        }
    }
}

void pos(){
    int tmin = 1e9, x = 0, y = 0;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(r[i][j] == jl[i][j] && r[i][j])
                if(tmin > r[i][j]){
                    tmin = r[i][j];
                    x = i;
                    y = j;
                }

    fout << tmin << " " << x << " " << y;
}

int main()
{
    read();
    calc(r, xr, yr);
    calc(jl, xj, yj);
    pos();

    return 0;
}