Cod sursa(job #2325807)

Utilizator iramIoana Popa iram Data 22 ianuarie 2019 22:31:47
Problema Rj Scor 100
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[102][102],JU[102][102],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 citeste()
{
    f>>N>>M;
    f.get();
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=M; j++)
        {
            f.get(ch); cout<<ch;
            if(ch=='R')
            {
                xr=i;
                yr=j;
            }
            if(ch=='J')
            {
                xj=i;
                yj=j;
            }
            if(ch=='X') ro[i][j]=JU[i][j]=-1;
        }
        f.get();cout<<endl;
    }
}

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;
            }
        }
    }
}

void drum_minim()
{
    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;
}

int main()
{
    citeste();
    lee(xr,yr,ro);
    lee(xj,yj,JU);
    drum_minim();
}