Cod sursa(job #2106893)

Utilizator ilie0712Botosan Ilie ilie0712 Data 16 ianuarie 2018 14:37:33
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.06 kb
#include <iostream>
#include <fstream>
#include <cstring>

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

const int N=110;

const int dl[]= {-1,0,1,0,-1,1,1,-1};
const int dc[]= {0,1,0,-1,1,1,-1,-1};
char a[N][N];
int d[N][N], n;
int e[N][N];

struct poz
{
    int l,c;
};

poz q[N*N+1];


int main()
{
    poz x,y;
    int st=0, dr=-1,m,y0,x0,h=110,n=110,o=110;

    in>>n>>ws;
    in>>m>>ws;

    for(int i=1; i<=n; ++i)
    {
        in.getline(1+a[i], N);
        for(int j=1; j<=m; ++j)
        {

            if(a[i][j]=='X')
            {
                d[i][j]=-2;
                e[i][j]=-2;
            }
            else if(a[i][j]=='R')
            {
                e[i][j]=-1;
                q[++dr]=(poz)
                {
                    i,j
                };
            }
            else if(a[i][j]=='J')
            {
                d[i][j]=-1;
                y0=j;
                x0=i;
            }
            else
            {
                d[i][j]=-1;
                e[i][j]=-1;
            }
        }
    }

    while(st<=dr)
    {
        x=q[st++];
        for(int i=0; i<8; ++i)
        {
            y.l=x.l+ dl[i];
            y.c=x.c+ dc[i];
            if(d[y.l][y.c]==-1)
            {
                q[++dr]=y;
                d[y.l][y.c]=d[x.l][x.c]+1;
            }

        }
    }

    dr=-1, st=0;
    q[++dr]=(poz){x0,y0};
    while(st<=dr)
    {
        x=q[st++];
        for(int i=0; i<8; ++i)
        {
            y.l=x.l+ dl[i];
            y.c=x.c+ dc[i];
            if(e[y.l][y.c]==-1 )
            {
                q[++dr]=y;
                e[y.l][y.c]=e[x.l][x.c]+1;
            }

        }
    }

    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
        {
            if(d[i][j]==e[i][j] && d[i][j]>0)
            {
               if(d[i][j]+1<m) h=d[i][j]+1;
               if(i<n) n=i;
               if(j<o) o=j;
            }

        }
        out<<h<<" "<<n<<" "<<o;
    return 0;
}