Cod sursa(job #1154318)

Utilizator roxana_97Soare Roxana Florentina roxana_97 Data 26 martie 2014 09:19:07
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.73 kb
#include<fstream>
#include<queue>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int n,m,romeo[110][110],julieta[110][110],xj,yj,xr,yr,p,q;
int dx[14]={0,-1,0,1,-1,1,-1,0,1};
int dy[14]={0,-1,-1,-1,0,0,1,1,1};
string s;
bool incoada[110][110];

struct point
{
    int x,y;
};

point b,curent;
queue < point > coada;

void lee(int xj,int yj)
{
    b.x=xj;
    b.y=yj;
    julieta[xj][yj]=1;
    coada.push(b); incoada[xj][yj]=true;
    while(!coada.empty())
    {
        curent=coada.front();
        coada.pop();
        incoada[curent.x][curent.y]=false;
        for(int k=1;k<=8;++k)
        {
            p=curent.x+dx[k];
            q=curent.y+dy[k];
            if(p>=1 && p<=n && q>=1 && q<=m &&(julieta[p][q]==0 || julieta[p][q]>julieta[curent.x][curent.y]+1))
            {

                b.x=p;
                b.y=q;
                julieta[p][q]=julieta[curent.x][curent.y]+1;
                if(incoada[p][q]==false)
                {
                    coada.push(b);
                    incoada[p][q]=true;
                }
            }
        }
    }
}

void lee2(int xr,int yr)
{
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)incoada[i][j]=false;
    b.x=xr;
    b.y=yr;
    romeo[xr][yr]=1;
    coada.push(b); incoada[xr][yr]=true;
    while(!coada.empty())
    {
        curent=coada.front();
        coada.pop();
        for(int k=1;k<=8;++k)
        {
            p=curent.x+dx[k];
            q=curent.y+dy[k];
            if(p>=1 && p<=n && q>=1 && q<=m &&(romeo[p][q]==0 || romeo[p][q]>romeo[curent.x][curent.y]+1))
            {

                b.x=p;
                b.y=q;
                romeo[p][q]=romeo[curent.x][curent.y]+1;
                if(incoada[p][q]==false)
                {
                    coada.push(b);
                    incoada[p][q]=true;
                }
            }
        }
    }
}


int main()
{
    f>>n>>m;
    getline(f,s);
    for(int i=1;i<=n;++i)
    {
        getline(f,s);
        for(int j=0;j<=m-1;++j)
        {
            if(s[j]=='X')
            {
                julieta[i][j+1]=-1;
                romeo[i][j+1]=-1;
            }
            if(s[j]=='J')
            {
                xj=i;yj=j+1;
            }
            if(s[j]=='R')
            {
                xr=i;yr=j+1;
            }
        }

    }
    lee(xj,yj);
    lee2(xr,yr);
    int minim=200000;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
         if(julieta[i][j]==romeo[i][j] && julieta[i][j]>0 && julieta[i][j]<minim)
         {
             minim=julieta[i][j];
             p=i;q=j;
         }
    g<<minim<<' '<<p<<' '<<q<<'\n';
    f.close();g.close();
    return 0;
}