Cod sursa(job #885814)

Utilizator sirangeorgeGeorge Siran sirangeorge Data 22 februarie 2013 13:09:11
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.71 kb
#include <fstream>

using namespace std;

ofstream out("rj.out");

const int dx[]={-1,-1,0,1,1,1,0,-1};
const int dy[]={0,1,1,1,0,-1,-1,-1};

int n,m,xr,yr,xj,yj,mat[100][100];

int cxr[10000], cyr[10000], cxj[10000], cyj[10000];

int lr,fr,lj,fj,pas,stop=1;

void citire()
{
    int i,j;
    char sir[101];
    ifstream in("rj.in");
    in>>n>>m;
    in.get();
    for(i=0;i<n;i++)
    {
        in.getline(sir,101,'\n');
        for(j=0;j<m;j++)
        {
            if(sir[j]=='R'){ xr=i; yr=j;}
            if(sir[j]=='J'){ xj=i; yj=j;}
            if(sir[j]=='X') mat[i][j]=1;
        }
    }
    in.close();
}
int bune(int ii,int jj)
{
    return((ii>=0)&&(ii<n)&&(jj>=0)&&(jj<m));
}

int main()
{
    int i,j,k,ii,jj,max,xretinut,yretinut, gasit;

    citire();
    mat[xr][yr]=1;
    mat[xj][yj]=1;
    cxr[0]=xr;
    cyr[0]=yr;
    cxj[0]=xj;
    cyj[0]=yj;
    lr=0;
    fr=0;
    lj=0;
    fj=0;
    /*
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
            out<<mat[i][j]<<" ";
        out<<endl;
    }
    out<<endl;
    */
    while((fr<=lr)&&(fj<=lj)&&stop)
    {
        for(k=0;k<8;k++)
        {
            ii=cxr[fr]+dx[k];
            jj=cyr[fr]+dy[k];
            if(bune(ii,jj))
            {
                pas=mat[cxr[fr]][cyr[fr]];
                if(mat[ii][jj]==pas+1) stop=0;
                else
                if ((mat[ii][jj]==0) ||(mat[ii][jj]>pas+1))
                {
                    pas=pas+1;
                    mat[ii][jj]=pas;
                    lr++;
                    cxr[lr]=ii;
                    cyr[lr]=jj;
                }
            }
        }
        fr++;
        for(k=0;k<8;k++)
        {
            ii=cxj[fj]+dx[k];
            jj=cyj[fj]+dy[k];
            if(bune(ii,jj))
            {
                pas=mat[cxj[fj]][cyj[fj]];
                if(mat[ii][jj]==pas+1) stop=0;
                else
                if( (mat[ii][jj]==0) ||(mat[ii][jj]>pas+1) )
                {
                    pas=pas+1;
                    mat[ii][jj]=pas;
                    lj++;
                    cxj[lj]=ii;
                    cyj[lj]=jj;
                }
            }
        }
        fj++;
    }

    max=0;
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
                if(mat[i][j]>max)
                    max=mat[i][j];

    gasit=1;
    for(i=0;i<n&&gasit;i++)
        for(j=0;j<m&&gasit;j++)
            if(mat[i][j]==max){gasit=0; xretinut=i; yretinut=j;}
    /*
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
            out<<mat[i][j]<<" ";
        out<<endl;
    }
    */
    out<<xretinut+1<<" "<<yretinut+1<<" "<<max;
    out.close();
    return 0;
}