Cod sursa(job #2635650)

Utilizator rares8wAncuta Rares rares8w Data 15 iulie 2020 11:34:43
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int R[105][105],J[105][105],n,m,i,j,nc_l,nc_c,nou_l,nou_c;
char x;
struct str
{
    int col,lin;
} aux;
queue <str> q;
queue <str> d;
int dl[8]= {1,-1,0,0,1,1,-1,-1}, dc[8]= {0,0,1,-1,-1,1,-1,1};
int main()
{
    f>>n>>m;
    for(i=0; i<=n+1; i++)
    {
        for(j=0; j<=m+1; j++)
            if(i==0 || j==0 || i==n+1 || j==m+1)
            {
                J[i][j]=-1;
                R[i][j]=-1;
            }
            else
            {
                f.get(x);
                if(x==' ')
                {
                    R[i][j]=0;
                    J[i][j]=0;
                }
                else if(x=='X')
                {
                    R[i][j]=-1;
                    J[i][j]=-1;
                }
                else if(x=='R')
                {
                    R[i][j]=1;
                    aux.lin=i;
                    aux.col=j;
                    q.push(aux);
                }
                else if(x=='J')
                {
                    J[i][j]=1;
                    aux.lin=i;
                    aux.col=j;
                    d.push(aux);
                }
            }
        f.get(x);
    }
    while(q.empty()==0)
    {
        nc_l=q.front().lin,nc_c=q.front().col;
        q.pop();
        for(i=0; i<8; i++)
        {
            nou_l=nc_l+dl[i],nou_c=nc_c+dc[i];
            if(R[nou_l][nou_c]==0)
            {
                R[nou_l][nou_c]=1+R[nc_l][nc_c];
                aux.lin=nou_l;
                aux.col=nou_c;
                q.push(aux);
            }
        }
    }
    while(d.empty()==0)
    {
        nc_l=d.front().lin;
        nc_c=d.front().col;
        d.pop();
        for(i=0; i<8; i++)
        {
            nou_l=nc_l+dl[i],nou_c=nc_c+dc[i];
            if(J[nou_l][nou_c]==0)
            {
                J[nou_l][nou_c]=1+J[nc_l][nc_c];
                aux.lin=nou_l;
                aux.col=nou_c;
                d.push(aux);
            }
        }
    }

    nc_c=n*m,nou_l=-1,nou_c=-1;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
            if(R[i][j]==J[i][j] && R[i][j]>0 && R[i][j]<nc_c)
            {
                nc_c=R[i][j];
                nou_l=i;
                nou_c=j;
            }
    g<<nc_c<<" "<<nou_l<<" "<<nou_c;
    return 0;
}