Cod sursa(job #2735399)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 2 aprilie 2021 12:52:23
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3 kb

#include <bits/stdc++.h>

using namespace std;

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

const int dl[] = {-1,0,1,0,1,-1,1,-1};
const int dc[] = {0,1,0,-1,1,-1,-1,1};
int a[105][105],b[105][105],n,m,d1[105][105],d2[105][105],xr,yr,xj,yj,imax,jmax;

struct poz
{
    int l,c;
};

void lee1()
{
    poz x,y;
    x.l = xr;
    x.c = yr;
    queue<poz>q;
    q.push(x);
    while (!q.empty())
    {
        x = q.front();
        a[x.l][x.c] = -1;
        for (int i = 0; i <= 7; i++)
        {
            y.l = x.l + dl[i];
            y.c = x.c + dc[i];
            if (a[y.l][y.c] == 1 and d1[y.l][y.c] == 0)
            {
                q.push(y);
                a[y.l][y.c] = -1;
                d1[y.l][y.c] = 1 + d1[x.l][x.c];
            }
        }
        q.pop();
    }
}

void lee2()
{
    poz x,y;
    x.l = xj;
    x.c = yj;
    queue<poz>q;
    q.push(x);
    while (!q.empty())
    {
        x = q.front();
        b[x.l][x.c] = -1;
        for (int i = 0; i <= 7; i++)
        {
            y.l = x.l + dl[i];
            y.c = x.c + dc[i];
            if (b[y.l][y.c] == 1 and d2[y.l][y.c] == 0)
            {
                q.push(y);
                b[y.l][y.c] = -1;
                d2[y.l][y.c] = 1 + d2[x.l][x.c];
            }
        }
        q.pop();
    }
}

int main()
{
    char s[105];
    int i,j,dmin = 100000;
    in >> n >> m;
    in.getline(s,5);
    for (i = 1; i <= n; i++)
    {
        in.getline(s,105);
        for (j = 0; j < strlen(s); j++)
        {
            if (s[j] == 'R')
            {
                xr = i;
                yr = j + 1;
                a[i][j + 1] = 2;
            }
            else if (s[j] == 'J')
            {
                xj = i;
                yj = j + 1;
                a[i][j + 1] = 3;
            }
            else if (s[j] == ' ')
                a[i][j + 1] = 1;
            else
                a[i][j + 1] = 0;
        }
        for (j = strlen(s) + 1; j <= m; j++)
            a[i][j] = 1;
        for (j = 1; j <= m; j++)
            b[i][j] = a[i][j];
    }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
            cout << a[i][j] << " ";
        cout << '\n';
    }
    cout << '\n';
    lee1();
    lee2();
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if (d1[i][j] == d2[i][j] and d1[i][j] < dmin and d1[i][j] != 0)
            {
                dmin = d1[i][j];
                imax = i;
                jmax = j;
            }
        }
    }
    out << dmin + 1 << " " << imax << " " << jmax;
    //cout << xr << " " << yr << " " << xj << " " << yj << '\n';
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
            cout << d1[i][j] << " ";
        cout << '\n';
    }
    cout << '\n';
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
            cout << d2[i][j] << " ";
        cout << '\n';
    }
    return 0;
}