Cod sursa(job #2275091)

Utilizator hoprixVlad Opris hoprix Data 2 noiembrie 2018 20:44:06
Problema Rj Scor 100
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int N, M, xr, yr, xj, yj, tmin = 105 * 105 + 5, xmin, ymin,nr;
char c;
int ro[105][105], ju[105][105];
int di[] = {-1,-1,0,1,1, 1, 0,-1};
int dj[] = { 0, 1,1,1,0,-1,-1,-1};
queue < pair<int,int> > coada;
void Lee(int x, int y, int m[105][105]){
    int i, j, ii, jj;
    m[x][y] = 1;
    coada.push(make_pair(x,y));
    while(!coada.empty()){
        i = coada.front().first;
        j = coada.front().second;
        coada.pop();
        for(int d=0; d<8; d++){
            ii = i + di[d];
            jj = j + dj[d];
            if(ii >= 1 && jj >= 1 && ii <= N && jj <= M && m[ii][jj] == 0){
                m[ii][jj] = m[i][j] + 1;
                coada.push(make_pair(ii,jj));
            }
        }
    }
}
int main(){
    fin>>N>>M;
    fin.get(c);
    for(int i=1; i<=N; i++){
        for(int j=1; j<=M; j++){
            fin.get(c);
            if(c == 'R'){
                xr = i;
                yr = j;
            }
            else if(c == 'J'){
                xj = i;
                yj = j;
            }
            else if(c == 'X')ro[i][j] = ju[i][j] = -1;
        }
        fin.get(c);
    }
    Lee(xr,yr,ro);
    Lee(xj,yj,ju);
    for(int i=1; i<=N; i++)
        for(int j=1; j<=M; j++)
            if(ro[i][j] == ju[i][j] && ro[i][j] < tmin && ro[i][j] > 0){
                tmin = ro[i][j];
                xmin = i;
                ymin = j;
            }
    fout<<tmin<<" "<<xmin<<" "<<ymin;
}