Cod sursa(job #2834166)

Utilizator David0911David Teregovan David0911 Data 16 ianuarie 2022 16:06:39
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");

int n, m, d1[102][102], d2[102][102], minim = 1e7, cf = 0, lf = 0;
char a[102][102];
struct poz
{
    int l, c;
};
queue<poz>q1;
queue<poz>q2;
poz start1, start2;
void leer()
{
    poz p1, p2;
    while(!q1.empty())
    {
        p1 = q1.front();
        for(int i = 0; i < 4; i++)
        {
            p2.l = p1.l + ln[i];
            p2.c= p1.c + cl[i];
            if(d1[p2.l][p2.c] == -1)
            {
                d1[p2.l][p2.c] = d1[p1.l][p1.c] + 1;
                q1.push(p2);
            }
        }
        q1.pop();
    }

}
void leej()
{
    poz p1, p2;
    while(!q2.empty())
    {
        p1 = q2.front();
        for(int i = 0; i < 4; i++)
        {
            p2.l = p1.l + ln[i];
            p2.c= p1.c + cl[i];
            if(d2[p2.l][p2.c] == -1)
            {
                d2[p2.l][p2.c] = d2[p1.l][p1.c] + 1;
                q2.push(p2);
            }
        }
        q2.pop();
    }
}
int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            fin >> a[i][j];
            if(a[i][j] == 'R')
            {
                d1[i][j] = 0;
                start1.l = i;
                start1.c = j;
                d2[i][j] = -1;
                q1.push(start1);
            }
            else if(a[i][j] == 'X')
            {
                d1[i][j] = -2;
                d2[i][j] = -2;
            }
            else if(a[i][j] == ' ')
            {
                d1[i][j] = -1;
                d2[i][j] = -1;
            }
            else
            {
                d1[i][j] = -1;
                d2[i][j] = 0;
                start2.l = i;
                start2.c = j;
                q2.push(start2);
            }
        }
    }
    leer();
    leej();
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(d1[i][j] == d2[i][j] && d1[i][j] > 0)
            {
                if(d1[i][j] < minim)
                {
                    minim = d1[i][j];
                    lf = i;
                    cf = j;
                }
            }
        }
    }
    fout << minim << "  " << lf << " " << cf;
    return 0;
}