Cod sursa(job #1647987)

Utilizator mirunazMiruna Zavelca mirunaz Data 10 martie 2016 23:14:22
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
int i,j,n,m,linie[8]={0,0,1,1,1,-1,-1,-1},coloana[8]={1,-1,-1,0,1,-1,0,1},lin,col,mn,x,y;
int vr[102][102],vj[102][102];
char c,s[102];
struct sheldon{int l,c;};
sheldon first,next,rom,jul;
queue <sheldon> q;
FILE *in, *out;
void lee_r()
{
    q.push(rom);
    vr[rom.l][rom.c]=1;
    while(!q.empty())
    {
        first=q.front();
        q.pop();
        int h;
        for(h=0;h<8;h++)
        {
            next.l=first.l+linie[h];
            next.c=first.c+coloana[h];
            if(vr[next.l][next.c]==-1)
            {
                q.push(next);
                vr[next.l][next.c]=vr[first.l][first.c]+1;
            }
        }
    }
}
void lee_j()
{
    q.push(jul);
    vj[jul.l][jul.c]=1;
    while(!q.empty())
    {
        first=q.front();
        q.pop();
        int t=vj[first.l][first.c];
        if(t==vr[first.l][first.c])
        {
            if(t<mn)
            {
                mn=t;
                x=first.l;
                y=first.c;
            }
        }
        int h;
        for(h=0;h<8;h++)
        {
            next.l=linie[h]+first.l;
            next.c=coloana[h]+first.c;
            if(vj[next.l][next.c]==-1)
            {
                q.push(next);
                vj[next.l][next.c]=vj[first.l][first.c]+1;
            }
        }
    }
}
int main ()
{
    in=fopen("rj.in","r");
    out=fopen("rj.out","w");
    fscanf(in,"%d %d%c",&n,&m,&c);
    mn=m*n;
    for(i=1;i<=n;i++)
    {
        fgets(s,102,in);
        for(j=1;j<=m;j++)
        {
            c=s[j-1];
            if(c!='X')
            {
                vr[i][j]=-1;
                vj[i][j]=-1;
                if(c=='R')
                {
                    rom.l=i;
                    rom.c=j;
                }
                else if(c=='J')
                {
                    jul.l=i;
                    jul.c=j;
                }
            }
        }
    }
    lee_r();
    lee_j();
    fprintf(out,"%d %d %d\n",mn,x,y);
    return 0;
}