Cod sursa(job #1258928)

Utilizator tudormaximTudor Maxim tudormaxim Data 9 noiembrie 2014 16:07:10
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <iostream>
#include <cstdio>
 #define nmax 105
using namespace std;
const int dx[]={0, -1, -1, 0, 1, 1, 1, 0, -1};
const int dy[]={0, 0, 1, 1, 1, 0, -1, -1, -1};
short mr[nmax][nmax], mj[nmax][nmax], n ,m, xj, yj, xr, yr;
int i, j;
void Lee(short mat[nmax][nmax], short x, short y)
{
    short qx[10003],qy[10003], inc, sf, nx, ny, xx, yy;
    inc=sf=1;
    qx[1]=x;
    qy[1]=y;
    mat[x][y]=1;
    while(inc<=sf)
    {
        nx=qx[inc];
        ny=qy[inc];
        inc++;
        for(i=1; i<=8; i++)
        {
            xx=nx+dx[i];
            yy=ny+dy[i];
            if(!mat[xx][yy])
            {
                sf++;
                qx[sf]=xx;
                qy[sf]=yy;
                mat[xx][yy]=mat[nx][ny]+1;
            }
        }
    }
}
int main()
{
    freopen("rj.in", "r", stdin);
    freopen("rj.out", "w", stdout);
    char s[nmax];
    cin>>n>>m;
    cin.getline(s,3,'\n');
    for(i=1; i<=n; i++)
    {
        cin.getline(s,nmax,'\n');
        for(j=0; j<m; j++)
        {
            if(s[j]=='R')
            {
                xr=i;
                yr=j+1;
            }
            if(s[j]=='J')
            {
                xj=i;
                yj=j+1;
            }
            if(s[j]=='X')
            {
                mr[i][j+1]=-1;
                mj[i][j+1]=-1;
            }
        }
    }
    for(i=0; i<=n+1; i++)
        mr[i][0]=mr[i][m+1]=mj[i][0]=mj[i][m+1]=-1;
    for(i=0;i<=m+1;i++)
        mr[0][i]=mr[n+1][i]=mj[0][i]=mj[n+1][i]=-1;
    Lee(mr, xr, yr);
    Lee(mj, xj, yj);
    int mn=m*n, xmn, ymn;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
            if(mr[i][j]>0 && mr[i][j]==mj[i][j] && mr[i][j]<mn)
            {
                mn=mr[i][j];
                xmn=i;
                ymn=j;
            }
    cout<<mn<<" "<<xmn<<" "<<ymn<<"\n";
    fclose(stdin);
    fclose(stdout);
    return 0;
}