Cod sursa(job #2325822)

Utilizator iramIoana Popa iram Data 22 ianuarie 2019 22:40:34
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;

ifstream f("rj.in");
ofstream g("rj.out");

int ro[101][101],ju[101][101],n,m,xr,yr,xj,yj;
char ch;

queue < pair<int,int> > c;

int dl[]= {0,1,1,1,0,-1,-1,-1},dc[]= {1,1,0,-1,-1,-1,0,1},tmin,xmin,ymin;

void citire()
{
    f>>n>>m;
    f.get();
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            f.get(ch);
            if(ch=='R')
            {
                xr=i;
                yr=j;
            }
            else if(ch=='J')
            {
                xj=i;
                yj=j;
            }
            else if(ch==' ') ro[i][j]=ju[i][j]=0;
            else if(ch=='X') ro[i][j]=ju[i][j]=-1;
        }
        f.get();
    }
}

bool ok(int i, int j)
{
    if(i<1 || i>n || j<1 || j>m ) return false;
    return true;

}

void lee(int x,int y, int a[102][102])
{
    int i,j,i_urm,j_urm,dir;

    while(!c.empty()) c.pop();
    c.push(make_pair(x,y));
    a[x][y]=1;
    while(!c.empty())
    {
        i=c.front().first;
        j=c.front().second;
        c.pop();
        for(dir=0; dir<8; dir++)
        {
            i_urm=i+dl[dir] ;
            j_urm=j+dc[dir];
            if(ok(i_urm,j_urm) && a[i_urm][j_urm]==0)
            {
                c.push(make_pair(i_urm,j_urm));
                a[i_urm][j_urm]=a[i][j]+1;
            }
        }
    }
}


int main()
{
    citire();
    lee(xr,yr,ro);
    lee(xj,yj,ju);
    tmin=101*101;
    xmin=ymin=0;

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(ro[i][j]==ju[i][j] && ro[i][j]<tmin && ro[i][j]>0)
            {
                tmin=ro[i][j];
                xmin=i;
                ymin=j;
            }
    g<<tmin<<" "<<xmin<<" "<<ymin<<endl;

}