Cod sursa(job #1993610)

Utilizator VarticeanNicolae Varticean Varticean Data 23 iunie 2017 13:08:12
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <fstream>
#include <string>
#include <queue>
using namespace std;
         ifstream in("rj.in");
         ofstream out("rj.out");
int A[105][105],N,M;
queue < pair<int, int> > Q ;
int di[8] = {0,0,1,1,1,-1,-1,-1};
int dj[8] = {1,-1,-1,0,1,-1,0,1};
string s;
pair < int,int > rasp;

int xj, yj, xr, yr;
void read()
{
    in>>N>>M;
    getline(in,s);
    for( int i=1; i<=N; i++)
    {
        getline(in,s);
        for(int j=0; j<M; j++)
    {
            if(s[j] == 'X' ) A[i][j+1]=-1;
            if(s[j] == 'J') { xj=i; yj=j+1; A[i][j+1]=1;}
            if(s[j] == 'R') { xr=i; yr=j+1; A[i][j+1]=1;}
    }

    }
}
bool OK(int i, int j )
{
   if( i<1 || j<1 || i>N || j>M ) return false ;
   if ( A[i][j] == -1 ) return false ;
     return true ;
}
void Leeju( int xj, int yj)
{
    int i,j,i_next,j_next;
    Q.push(make_pair(xj,yj));
    while( !Q.empty() )
    {
        i = Q.front().first;
        j = Q.front().second;
        Q.pop();
    for( int dr=0; dr<8; dr++)
    {
        i_next=i+di[dr];
        j_next=j+dj[dr];
        if( OK(i_next, j_next ) && !A[i_next][j_next] )
        {
            A[i_next][j_next]=A[i][j]+1;
            Q.push(make_pair(i_next,j_next));
        }
    }
    }
}
void Leero( int xr, int yr)
{
    int i,j,i_next,j_next;
     int minim=0;
    Q.push(make_pair(xr,yr));
    while( !Q.empty() )
    {
        i = Q.front().first;
        j = Q.front().second;
        Q.pop();
    for( int dr=0; dr<8; dr++)
    {
        i_next=i+di[dr];
        j_next=j+dj[dr];
        if( OK(i_next, j_next ) && A[i][j]<A[i_next][j_next] )
        {
            A[i_next][j_next]=A[i][j]+1;
            Q.push(make_pair(i_next,j_next));
        } else if ( OK(i_next, j_next ) && A[i_next][j_next] < A[i][j]  )
        { minim=A[i_next][j_next]; rasp.first=i_next; rasp.second=j_next;}
    }
    }
}
int main()
{
     read();
     Leeju(xj,yj);
     Leero(xr,yr);
      for( int i=1; i<=N; i++)
      {
          for(int j=1; j<=M; j++)
            out<<A[i][j]<<' ';
          out<<'\n';
      }
out<<rasp.first<<' '<<rasp.second<<' '<<A[rasp.first][rasp.second];
    return 0;
}