Cod sursa(job #1360008)

Utilizator redducks100Andronache Simone redducks100 Data 25 februarie 2015 10:47:38
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

ifstream f("rj.in");
ofstream g("rj.out");

char a[102][102];
int m,n,xr,yr,xj,yj;
int dx[8]={0, 1, 0, -1, -1, 1, -1, 1};
int dy[8]={1, 0, -1, 0, -1, 1,  1,-1};
int r[102][102];

struct pct {
    int x,y;
};

void lee(int x0,int y0,int d[102][102])
{
    queue<pct> Q;
    pct start,now;
    for(int i=0;i<=n+1;i++)
        for(int j=0;j<=m+1;j++)
            d[i][j]=-1;
    start.x=x0;
    start.y=y0;
    Q.push(start);
    d[x0][y0]=1;
    while(Q.empty() == false)
    {
        now=Q.front();
        Q.pop();
        int xx,yy;
        for(int i=0;i<8;i++)
        {
            xx=now.x+dx[i];
            yy=now.y+dy[i];
            if(a[xx][yy]==' ' && d[xx][yy]==-1)
            {
                d[xx][yy]=1+d[now.x][now.y];
                pct now2;
                now2.x=xx;
                now2.y=yy;
                Q.push(now2);
            }
        }
    }
}

int main()
{
    int ju[102][102];
    char c;
    f>>n>>m;
    for (int i=0; i<=n+1; i++)
        a[i][0]=a[i][m+1]='X';
    for (int i=0; i<=m+1; i++)
        a[0][i]=a[n+1][i]='X';
    f.get(c);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            f.get(c);
            a[i][j]=c;
            if(a[i][j]=='R')
            {
                xr=i;
                yr=j;
                a[i][j]=' ';
            }
            if(a[i][j]=='J')
            {
                xj=i;
                yj=j;
                a[i][j]=' ';
            }
        }
        f.get(c);
    }
    lee(xr,yr,r);
    lee(xj,yj,ju);
    int tmin=102*102+5, xmin=-1, ymin=-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(r[i][j]==ju[i][j])
                if(r[i][j]<tmin && r[i][j]!=-1)
                {
                    tmin=r[i][j];
                    xmin=i;
                    ymin=j;
                }
        }
    }
    g<<tmin<<" "<<xmin<<" "<<ymin<<"\n";
    return 0;
}