Cod sursa(job #1805898)

Utilizator LaurIleIle Laurentiu Daniel LaurIle Data 14 noiembrie 2016 17:21:11
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.64 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <string.h>
using namespace std;

int n, m, tmin=1000010, ox, oy, ix1, iy1, ix2, iy2, romeo[107][107], julieta[107][107];
char a[105][105];
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;   f.get();
    for(int i=1; i<=n; ++i)
    {
        f.getline(a[i]+1,m+1);
        for(int j=1; j<=m; ++j)
        {
            if(a[i][j]=='R')
                {ix1=i;
                 iy1=j; romeo[i][j]=1;
                }
                else
            if(a[i][j]=='J')
                { ix2=i;
                  iy2=j; julieta[i][j]=1;
                }
                else
            if(a[i][j]=='X')
                {
                romeo[i][j]=-1;
                julieta[i][j]=-1;
                }
        }
    }

    for(int i=0; i<=n+1; ++i) romeo[i][0]=romeo[i][n+1]=julieta[i][0]=julieta[i][n+1]=-1;
    for(int i=0; i<=m+1; ++i) romeo[0][i]=romeo[m+1][i]=julieta[0][i]=julieta[m+1][i]=-1;

    f.close();
}

void lee1()
{
      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(romeo[yy.first][yy.second] == 0)
              {
                  romeo[yy.first][yy.second] = romeo[xx.first][xx.second] + 1;
                  q.push(make_pair(yy.first, yy.second));
              }
          }
      }
}

void lee2()
{
      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(julieta[yy.first][yy.second] == 0)
              {
                  julieta[yy.first][yy.second] = julieta[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(romeo[i][j]==julieta[i][j] && romeo[i][j]>0 && romeo[i][j]<tmin)
            {
                ox=i; oy=j; tmin=romeo[i][j];
            }
        }
    }
}

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

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