Cod sursa(job #2833986)

Utilizator andreea_eliza_8Radu Andreea Eliza andreea_eliza_8 Data 16 ianuarie 2022 10:39:29
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int dirlin[]={-1,0,1,1,1,0,-1,-1};
int dircol[]={-1,-1,-1,0,1,1,1,0};
int n, m, jx, jy, rx, ry, rom[205][205], jul[205][205];

struct Coord
{
    int row;
    int col;
};

bool este(int x, int y)
{
    return x>=1 && y>=1 && x<=n && y<=m;
}

void lee(int xinceput, int yinceput, int mat[205][205])
{
    queue <Coord> leeQueue;
    Coord popas;
    popas.row=xinceput;
    popas.col=yinceput;
    leeQueue.push(popas);
    mat[popas.row][popas.col]=1;
    while(!leeQueue.empty())
    {
        Coord frn=leeQueue.front();
        leeQueue.pop();
        for(int i=0; i<8; i++)
        {
            Coord ngb;
            ngb.row=frn.row+dirlin[i];
            ngb.col=frn.col+dircol[i];
            if(este(ngb.row, ngb.col) && mat[ngb.row][ngb.col]==0)
            {
                mat[ngb.row][ngb.col]=mat[frn.row][frn.col]+1;
                leeQueue.push(ngb);
            }
        }
    }
}

int main()
{
    fin>>n>>m;
    string gol;
    getline(fin,gol);
    for(int i=1; i<=n; i++)
    {
        string line;
        getline(fin, line);
        for (int j=1; j<=m; j++)
        {
            char chr=line[j-1];
            if (chr=='X')
            {
                rom[i][j]=-1;
                jul[i][j]=-1;
            }
            else if (chr=='J')
            {
                jx=i, jy=j;
            }
            else if (chr=='R')
            {
                rx=i, ry=j;
            }
        }
    }
    lee(rx, ry, rom);
    lee(jx, jy, jul);
    int x=0, y=0, tmin=2147483647;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(jul[i][j]==rom[i][j] && rom[i][j]>0 && jul[i][j]>0)
            {
                if(jul[i][j]<tmin)
                {
                    x=i;
                    y=j;
                    tmin=rom[i][j];
                }
            }
        }
    }
    fout<<tmin<<" "<<x<<" "<<y;
    return 0;
}