Cod sursa(job #3211383)

Utilizator Mihai_AritonMihai Ariton Mihai_Ariton Data 9 martie 2024 11:07:52
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <bits/stdc++.h>
using namespace std;

queue<pair<int, int>>q;
int fj[105][105], fr[105][105], v[105][105];
int distj[105][105], distr[105][105];
int dirl[]={-1, -1, 0, 1, 1, 1, 0, -1};
int dirc[]={0, 1, 1, 1, 0, -1, -1, -1};
int main()
{
    ifstream cin("rj.in");
    ofstream cout("rj.out");
    
    int n, m, lj, cj, lr, cr, x, y, tmin;
    char ch;
    cin>>n>>m;cin.get(ch);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin.get(ch);
            if(ch=='X')
            v[i][j]=1;
            else if(ch=='R')
            {
                lr=i;
                cr=j;
            }
            else if(ch=='J')
            {
                lj=i;
                cj=j;
            }
        }
        cin.get(ch);
    }
    q.push({lr, cr});fr[lr][cr]=1;distr[lr][cr]=1;
    while(!q.empty())
    {
        pair<int, int>crn=q.front();q.pop();
        lr=crn.first;cr=crn.second;
        for(int i=0; i<=7; i++)
        {
            x=lr+dirl[i];y=cr+dirc[i];
            if(x>=1 && x<=n && y>=1 && y<=m && fr[x][y]==0 && v[x][y]==0)
            {
                q.push({x, y});
                fr[x][y]=1;
                distr[x][y]=distr[lr][cr]+1;
            }
        }
    }
    q.push({lj, cj});fj[lj][cj]=1;distj[lj][cj]=1;
    while(!q.empty())
    {
        pair<int, int>crn=q.front();q.pop();
        lj=crn.first;cj=crn.second;
        for(int i=0; i<=7; i++)
        {
            x=lj+dirl[i];y=cj+dirc[i];
            if(x>=1 && x<=n && y>=1 && y<=m && fj[x][y]==0 && v[x][y]==0)
            {
                q.push({x, y});
                fj[x][y]=1;
                distj[x][y]=distj[lj][cj]+1;
            }
        }
    }
    x=1;y=1;tmin=1e9;
    //cout<<distr[4][4]<<endl;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(distr[i][j]==distj[i][j] && distr[i][j]<tmin && fr[i][j]==1 && fj[i][j]==1)
            {
                x=i;y=j;
                tmin=distr[i][j];
            }
        }
    }
    cout<<tmin<<" "<<x<<" "<<y;

    return 0;
}