Cod sursa(job #2952117)

Utilizator divadddDavid Curca divaddd Data 8 decembrie 2022 13:00:22
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <iostream>
#include <climits>
#include <fstream>
#include <queue>
#define MAX 102
using namespace std;
int n,m,v[MAX][MAX],ro[MAX][MAX],ju[MAX][MAX],xr,yr,xj,yj;
string str;

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

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

bool inauntru(int x, int y){
    return ( (1 <= x && x <= n) && (1 <= y && y <= m) );
}

void lee(int x, int y, int vf[MAX][MAX]){
    queue<pair<int, int>> cd;
    cd.push({x, y});
    vf[x][y] = 1;
    while(!cd.empty()){
        x = cd.front().first;
        y = cd.front().second;
        cd.pop();
        for(int k = 0; k < 8; k++){
            int xnou = x+dx[k];
            int ynou = y+dy[k];
            if(inauntru(xnou, ynou) && vf[xnou][ynou] == 0){
                vf[xnou][ynou] = 1+vf[x][y];
                cd.push({xnou, ynou});
            }
        }
    }
}

int main()
{
    fin >> n >> m;
    fin.ignore();
    for(int i = 1; i <= n; i++){
        getline(fin, str);
        for(int j = 0; j < str.size(); j++){
            if(str[j] == 'R'){
                v[i][j+1] = 0;
                xr = i;
                yr = j+1;
            }else if(str[j] == 'J'){
                v[i][j+1] = 0;
                xj = i;
                yj = j+1;
            }else if(str[j] == ' '){
                v[i][j+1] = 0;
            }else{
                v[i][j+1] = 1;
                ro[i][j+1] = 1;
                ju[i][j+1] = 1;
            }
        }
    }
    lee(xr, yr, ro);
    lee(xj, yj, ju);
    int ans = INT_MAX, posx = 0, posy = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(ro[i][j] == ju[i][j] && ro[i][j] != 0){
                if(ro[i][j] < ans){
                    ans = ro[i][j];
                    posx = i;
                    posy = j;
                }
            }
        }
    }
    fout << ans << " " << posx << " " << posy;
    return 0;
}