Cod sursa(job #1640910)

Utilizator valorosu_300Cristian Gherman valorosu_300 Data 8 martie 2016 19:54:03
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <fstream>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
const int nmax=101;
int rmat[nmax][nmax],jmat[nmax][nmax];
int inauntru(int ii, int jj,int n, int m)
{
    return(ii>=0 && ii<n && jj>=0 && jj<m);
}
void chinezu(int n,int m, int mat[][101],int xi, int yi)
{
    int ii,jj,cx[10000],cy[10000],pi,ps;
    cx[0]=xi;
    cy[0]=yi;
    pi=0;
    ps=0;
    int dx[]={1,0,-1,0,-1,-1,1,1};
    int dy[]={0,1,0,-1,-1,1,-1,1};
    mat[xi][yi]=1;
    while(pi<=ps)
    {
        for(int k=0;k<8;k++)
        {
            ii=cx[pi]+dx[k];
            jj=cy[pi]+dy[k];
            if(inauntru(ii,jj,n,m)==1)
            if(mat[ii][jj]==0)
            {
                mat[ii][jj]=mat[cx[pi]][cy[pi]]+1;
                ps++;
                cx[ps]=ii;
                cy[ps]=jj;
            }
        }
        pi++;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n,m,xi,yi,xf,yf,t=nmax*nmax,c1,c2;
    char x[nmax];
    in>>n>>m;
    in.get();///habar n-am de ce se foloseste asta
    for(int i=0;i<n;i++)
        {
            in.getline(x,nmax,'\n');
            for(int j=0;j<m;j++)
                {
                    if(x[j]=='X')
                        {
                            rmat[i][j]=1;
                            jmat[i][j]=1;
                        }
                    if(x[j]=='R')
                        {
                            xi=i;
                            yi=j;
                        }
                    if(x[j]=='J')
                        {
                            xf=i;
                            yf=j;
                        }///dinspre rromeo spre julita
                }
        }
    in.close();
    chinezu(n,m,rmat,xi,yi);
    chinezu(n,m,jmat,xf,yf);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if((rmat[i][j]>1)&&(rmat[i][j]==jmat[i][j]))
                if(rmat[i][j]<t)
                    {
                        t=rmat[i][j];
                        c1=i;
                        c2=j;
                    }
    c1++;
    c2++;
    out<<t<<" "<<c1<<" "<<c2;
    out.close();
    return 0;
}