Cod sursa(job #1120670)

Utilizator roxana_97Soare Roxana Florentina roxana_97 Data 25 februarie 2014 09:19:22
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
#include<fstream>
#include<queue>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int n,m,a[110][110],x1,y1,x2,y2,p,q,r[110][110];
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 x1,int y1)
{
    b.x=x1;
    b.y=y1;
    a[x1][y1]=1;
    coada.push(b);
    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 &&(a[p][q]==0 || a[p][q]>a[curent.x][curent.y]+1))
            {

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

void lee2(int x2,int y2)
{
    bool incoada[110][110];
    b.x=x2;
    b.y=y2;
    r[x2][y2]=1;
    coada.push(b);
    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 &&(r[p][q]==0 || r[p][q]>r[curent.x][curent.y]+1))
            {

                b.x=p;
                b.y=q;
                r[p][q]=r[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')
            {
                a[i][j+1]=-1;
                r[i][j+1]=-1;
            }
            //if(s[i]==' ') a[i][j]=0;
            if(s[j]=='J')
            {
                x1=i;y1=j+1;
            }
            if(s[j]=='R')
            {
                x2=i;y2=j+1;
            }
        }

    }
    lee(x1,y1);
    lee2(x2,y2);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
         if(a[i][j]==r[i][j] && a[i][j]>0) g<<a[i][j]<<' '<<i<<' '<<j<<'\n';
    f.close();g.close();
    return 0;

}