Cod sursa(job #2061891)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 9 noiembrie 2017 19:59:33
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.52 kb
#include <fstream>

using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");
struct punct {
    int l,c;
};
char c;
int dx[10]={-1,-1,-1,0,0,1,1,1};
int dy[10]={-1,0,1,-1,1,-1,0,1};
punct q[10002];
int n,i,m,j,p,u,x,y,xir,yir,xij,yij,xx,yy,col,lin,tmin,z,ju[105][105],r[105][105];
int main()
{
    f>>n>>m;
    f.get(c);
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
        {
            f.get(c);
            if (c=='R')
            {
                xir=i;
                yir=j;
                r[i][j]=1;
            }
            else if (c=='J')
            {
                xij=i;
                yij=j;
                ju[i][j]=1;
            }
            else if (c==' ')
            {
                ju[i][j]=r[i][j]=0;
            }
            else if (c=='X')
            {
                ju[i][j]=r[i][j]=-1;
            }
            else if (c=='\n')
            {
                ju[i][j]=r[i][j]=0;
            }
        }
        if (c!='\n') f.get(c);
    }
    for(i = 0; i <= n + 1; i++)
        r[i][0] = r[i][m + 1] = -1;
    for(i = 0; i <= m + 1; i++)
        r[0][i] = r[n + 1][i] = -1;
    for(i = 0; i <= n + 1; i++)
        ju[i][0] = ju[i][m + 1] = -1;
    for(i = 0; i <= m + 1; i++)
        ju[0][i] = ju[n + 1][i] = -1;
    p=1;
    u=1;
    q[1].l=xir;
    q[1].c=yir;
    while (p<=u)
    {
        x=q[p].l;
        y=q[p].c;
        for (i=0;i<8;i++)
        {
            xx=x+dx[i];
            yy=y+dy[i];
            if (r[xx][yy]==0)
            {
                r[xx][yy]=r[x][y]+1;
                u++;
                q[u].l=xx;
                q[u].c=yy;
            }
        }
        p++;
    }
    p=1;
    u=1;
    q[1].l=xij;
    q[1].c=yij;
    while (p<=u)
    {
        x=q[p].l;
        y=q[p].c;
        for (i=0;i<8;i++)
        {
            xx=x+dx[i];
            yy=y+dy[i];
            if (ju[xx][yy]==0)
            {
                ju[xx][yy]=ju[x][y]+1;
                u++;
                q[u].l=xx;
                q[u].c=yy;
            }
        }
        p++;
    }
    tmin=10000000000;
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
        {
            if (r[i][j]==ju[i][j] && r[i][j]!=-1 && r[i][j]!=0)
            {
                if (tmin>r[i][j]) {
                    tmin=r[i][j];
                    lin=i;
                    col=j;
                }
            }
        }
    }
    g<<tmin<<" "<<lin<<" "<<col<<'\n';
    return 0;
}