Cod sursa(job #1805588)

Utilizator LaurIleIle Laurentiu Daniel LaurIle Data 13 noiembrie 2016 23:01:13
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.59 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

int n, m, tmin=155, ox, oy, a[155][155], ix1, iy1, ix2, iy2, b[155][155];
queue <pair <int, int> > q;
const int dx[]={0,1,1,1,0,-1,-1,-1};
const int dy[]={1,1,0,-1,-1,-1,0,1};

void read()
{
    ifstream f("rj.in");
    f >> n >> m;  char aux[105], c;
    for(int i=1; i<=n; ++i)
    {   f.get();
        f.get(aux,101);
        for(int j=0; j<m; ++j)
        {
            c=aux[j];
            if(c=='R')
                {ix1=i;
                 iy1=j+1;
                 b[i][j+1]=1;
                 a[i][j+1]=1;}
            if(c=='J')
                { ix2=i;
                  iy2=j+1;
                  b[i][j+1]=1;
                  a[i][j+1]=1;}
            if(c=='X')
            {
                a[i][j+1]=-1;
                b[i][j+1]=-1;
            }
        }
    }
    f.close();
}

void lee1()
{
      for(int i=0; i<=n+1; ++i) a[i][0]=a[i][n+1]=-1;
      for(int i=0; i<=m+1; ++i) a[0][i]=a[m+1][i]=-1;
      pair <int, int> xx, yy;
      q.push(make_pair(ix1, iy1));
      while(!q.empty())
      {
          xx=q.front(); q.pop();
          for(int i=0; i<8; ++i)
          {
              yy.first = xx.first + dx[i];
              yy.second = xx.second +dy[i];
              if(a[yy.first][yy.second] == 0)
              {
                  a[yy.first][yy.second] = a[xx.first][xx.second] + 1;
                  q.push(make_pair(yy.first, yy.second));
              }
          }
      }
}

void lee2()
{
    for(int i=0; i<=n+1; ++i) b[i][0]=b[i][n+1]=-1;
    for(int i=0; i<=m+1; ++i) b[0][i]=b[m+1][i]=-1;
      pair <int, int> xx, yy;
      q.push(make_pair(ix2, iy2));
      while(!q.empty())
      {
          xx=q.front(); q.pop();
          for(int i=0; i<8; ++i)
          {
              yy.first = xx.first + dx[i];
              yy.second = xx.second +dy[i];
              if(b[yy.first][yy.second] == 0)
              {
                  b[yy.first][yy.second] = b[xx.first][xx.second] + 1;
                  q.push(make_pair(yy.first, yy.second));
              }
          }
      }
}

void cauta()
{
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            if(a[i][j]==b[i][j] && a[i][j]>1 && a[i][j]<tmin)
            {
                ox=i; oy=j; tmin=a[i][j];
            }
        }
    }
}

void out()
{
    ofstream g("rj.out");
    g << tmin <<' '<< ox <<' '<< oy <<'\n';
    g.close();
}

int main()
{
    read();
    lee1();
    lee2();
    cauta();
    out();
    return 0;
}