Cod sursa(job #1805517)

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

int n, m, tmin=155, a[155][155], xo=155, yo=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')
                {q.push(make_pair(i,j+1)); a[i][j+1]=1;}
            if(c=='J')
                {q.push(make_pair(i,j+1)); a[i][j+1]=1;}
            if(c==' ')
                a[i][j+1]=0;
            if(c=='X')
                a[i][j+1]=-1;
        }
    }
    f.close();
}

void lee()
{
      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;
      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));
              }
              else
              if(a[yy.first][yy.second] == a[xx.first][xx.second] + 1)
              {
                  if(yy.first < xo)
                  {
                      xo = yy.first;
                      yo = yy.second;
                      tmin = a[yy.first][yy.second];
                  }
                  else
                  if(yy.second < yo && yy.first == xo)
                  {
                      xo = yy.first;
                      yo = yy.second;
                      tmin = a[yy.first][yy.second];
                  }
              }
          }
      }

}

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

int main()
{
    read();
    lee();
    out();
    return 0;
}