Cod sursa(job #1883844)

Utilizator andreiutu111Noroc Andrei Mihail andreiutu111 Data 18 februarie 2017 11:33:37
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include    <fstream>
#include    <cstring>
#include    <queue>
#include    <cmath>


using namespace std;


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


int di[9]={0, 0, -1, 1, -1, 1, -1, 1};
int dj[9]={1, -1, 0, 0, -1, 1, 1, -1};

int MapR[101][101],MapJ[101][101], N, M;
int startx, starty, stopx, stopy, fx, fy, Min=9999999;

queue < pair< int, int > > coada;

void Read(){
    char p[101];

    fin>> N >> M;
    fin.get();
    for(int i=1;i<=N;i++){

            fin.getline(p,101);
            for(int l=0;l<strlen(p);l++)
                if(p[l]=='X')MapJ[i][l+1]=MapR[i][l+1]=-1;
                else if(p[l]=='R')startx=i,starty=l+1;
                else if(p[l]=='J')stopx=i,stopy=l+1;

    }

}

bool OK(int i,int j,int Map[101][101]){

    if(i<1 || j<1 || i>N || j>M)
            return false;

    if(Map[i][j]==-1 || (startx==i && starty==j))
            return false;

    return true;

}

void Lee(int Map[101][101]){

    int i, j ,i_next , j_next;
    coada.push( make_pair(startx , starty) );

    while( !coada.empty() ){

        i = coada.front().first;
        j = coada.front().second;
        coada.pop();

        for(int dir = 0 ; dir < 9 ; dir++){

            i_next = i + di[dir];
            j_next = j + dj[dir];

            if(OK(i_next,j_next,Map) && Map[i_next][j_next]<1){
                coada.push( make_pair(i_next,j_next) );
                Map[i_next][j_next] = Map[i][j] + 1;
            }

        }

    }

}

void checkPoz(int &fx,int &fy,int &Min){

    for(int i=1;i<=N;i++)
        for(int j=1;j<=M;j++)
            if(MapJ[i][j]==MapR[i][j] && MapJ[i][j]<Min && MapJ[i][j]>0)
                    Min=MapJ[i][j],fx=i,fy=j;

}

int main()
{
    Read();
    Lee(MapR);
    swap(startx,stopx),swap(starty,stopy);
    Lee(MapJ);
    checkPoz(fx,fy,Min);
    fout<<Min+1<<" "<<fx<<" "<<fy;

}