Cod sursa(job #1859740)

Utilizator alexandruilieAlex Ilie alexandruilie Data 27 ianuarie 2017 20:09:24
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int a[102][102],m,n,i,j,x2,y2,ok=1,ln,cl,cc,ll,nr,ok2=0,x3,y3,q1,q2,b[102][102],dmin=10001;
int dx[8]={0,0,1,-1,-1,-1,1,1};
int dy[8]={1,-1,0,0,-1,1,-1,1};
char c;
struct cv{int x,y;}ceva;
queue <cv> v;
queue <cv> v2;
int main()
{
   f>>n>>m;
   for(i=1;i<=n;i++)
   {
       if(c!='\n') f.get(c);
       for(j=1;j<=m;j++)
       {
           f.get(c);
           if(c=='X') b[i][j]=a[i][j]=-1;
           if(c=='R') ceva.x=x3=i,ceva.y=y3=j;
           if(c=='J') x2=i,y2=j;
       }
   }
   a[ceva.x][ceva.y]=2;
   v.push(ceva);
   for(i=0;i<=n+1;i++)
    b[i][0]=b[i][m+1]=a[i][0]=a[i][m+1]=-1;
   for(i=0;i<=m+1;i++)
    b[0][i]=b[n+1][i]=a[0][i]=a[n+1][i]=-1;
   while(!v.empty()&&ok)
   {
       ln=v.front().x;
       cl=v.front().y;
       for(i=0;i<8;i++)
       {ll=ln+dx[i];
       cc=cl+dy[i];
       if(a[ll][cc]==0)
        {
            if(dx[i]==dy[i]||dx[i]==-dy[i])ok2=1;
            ceva.x=ll;
            ceva.y=cc;
            v.push(ceva);
            a[ll][cc]=a[ln][cl]+1;
            if(cl==y2&&ll==x2) {nr=a[ll][cc];ok=0;break;}
        }
       }
       v.pop();

   }
   ok=1;
   ceva.x=x2;ceva.y=y2;
    b[ceva.x][ceva.y]=2;
   v2.push(ceva);
    while(!v2.empty()&&ok)
   {
       ln=v2.front().x;
       cl=v2.front().y;
       for(i=0;i<8;i++)
       {ll=ln+dx[i];
       cc=cl+dy[i];
       if(b[ll][cc]==0)
        {
            if(dx[i]==dy[i]||dx[i]==-dy[i])ok2=1;
            ceva.x=ll;
            ceva.y=cc;
            v2.push(ceva);
            b[ll][cc]=b[ln][cl]+1;
            if(cl==y3&&ll==x3) {nr=b[ll][cc];ok=0;break;}
        }
       }
       v2.pop();

   }
   /*ok=1;
    for(i=1;i<=n&&ok;i++)
       {for(j=1;j<=m;j++)
       g<<a[i][j]<<' ';
       g<<'\n';
       }
       g<<'\n';
        for(i=1;i<=n&&ok;i++)
       {for(j=1;j<=m;j++)
       g<<b[i][j]<<' ';
       g<<'\n';
       }*/
   ok=1;
    for(i=1;i<=n&&ok;i++)
       for(j=1;j<=m;j++)
        if(a[i][j]==b[i][j]&&a[i][j]>0) {
            if(a[i][j]<dmin) {dmin=a[i][j];
                q1=i;q2=j;}
        }
        g<<dmin-1<<' '<<q1<<' '<<q2;
    return 0;
}