Cod sursa(job #3157332)

Utilizator AndreiKatsukiAndrei Dogarel AndreiKatsuki Data 15 octombrie 2023 12:54:33
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("rj.in");
ofstream g("rj.out");

const int NMAX = 105;
int romeo[NMAX][NMAX], julieta[NMAX][NMAX], a[NMAX][NMAX], n, m;
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

struct Pozitie{
    int x, y;
}pozRomeo, pozJulieta;

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

void lee(int path[NMAX][NMAX], int i, int j){
    queue<pair<int, int>> q;
    q.push({i, j});
    while(!q.empty()){
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for(int d = 0; d < 8; ++d){
            int inou = x + dx[d];
            int jnou = y + dy[d];
            if(inMat(inou, jnou) && a[inou][jnou] == 0 && path[inou][jnou] == 0){
                path[inou][jnou] = path[x][y] + 1;
                q.push({inou, jnou});
            }
        }
    }
}

int main(){
    f >> n >> m;
    f.get();
    char ch;
    for(int i = 1; i <= n; ++i){
        f.get();
        for(int j = 1; j <= m; ++j){
            f.get(ch);
            if(ch == 'R'){
                pozRomeo.x = i;
                pozRomeo.y = j;
                romeo[i][j] = 1;
            }
            if(ch == 'J'){
                pozJulieta.x = i;
                pozJulieta.y = j;
                julieta[i][j] = 1;
            }
            if(ch == 'X'){
                a[i][j] = -1;
            }
        }
    }
    lee(romeo, pozRomeo.x, pozRomeo.y);
    lee(julieta, pozJulieta.x, pozJulieta.y);
    int res = 1e9, x, y;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j){
            if(romeo[i][j] == julieta[i][j] && res > romeo[i][j] && romeo[i][j]){
                res = romeo[i][j];
                x = i;
                y = j;
            }
        }
    }
    g << res << " " << x << " " << y;
    return 0;
}