Cod sursa(job #2702294)

Utilizator As932Stanciu Andreea As932 Data 3 februarie 2021 16:52:32
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <fstream>
#include <queue>
#define per pair<int,int>

using namespace std;

ifstream cin("rj.in");
ofstream cout("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(){
    cin >> n >> m;
    cin.get();

    for(int i = 1; i <= n; i++){
        cin.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;
        }
    }


    /**for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++)
            cout << aa[i][j];
        cout << "\n";
    }*/
}

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 <per> q;
    q.push({l, c});

    while(!q.empty()){
        per 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;
                }

    cout << tmin << " " << x << " " << y;
}

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

    return 0;
}