Cod sursa(job #1255119)

Utilizator patrutoiuandreipatrutoiu andrei patrutoiuandrei Data 4 noiembrie 2014 13:05:18
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.03 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("rj.in");
ofstream fout ("rj.out");
int q[2][100001],Min=100000,h[102][102],r[102][102],a[102][102],OK,cj,lj,cr,lr,j,n,m,i,l,c,ch,sol1,sol2;
int dx[]={-1,-1,0,1,1,1,0,-1};
int dy[]={0,1,1,1,0,-1,-1,-1};
void coada (int x, int y)
{
    int p,u;
    p=u=1;
    q[0][p]=x;
    q[1][p]=y;
    while(p<=u)
    {
        x=q[0][p];
        y=q[1][p];
        for(int i=0;i<8;i++)
        {
            int l=x+dx[i];
            int c=y+dy[i];
            if (a[l][c]==0&&r[l][c]==0&&OK==1)
            {
                q[0][++u]=l;
                q[1][u]=c;
                r[l][c]=r[x][y]+1;
            }
            else if (a[l][c]==0&&h[l][c]==0&&OK==2)
            {
                q[0][++u]=l;
                q[1][u]=c;
                h[l][c]=h[x][y]+1;
            }
        }
        p++;
    }

}
int main()
{

    fin>>n>>m;
    for(i=0;i<=n+1;i++)
        {
            a[i][0]=-1;
            a[i][m+1]=-1;
        }
    for (j=0;j<=m+1;j++)
        {
            a[0][j]=-1;
            a[n+1][j]=-1;
        }
int ok=1;
    for(i=1;i<=n;i++){
        if(ok==1)
        {
            fin.get();

        }
        ok=1;
        for(j=1;j<=m;j++)
        {
            ch=fin.get();
            if (ch=='X')
                a[i][j]=-1;
            else
            if (ch==' ')
                a[i][j]=0;
             else
            if (ch=='R')
            {

                lr=i;
                cr=j;
                r[i][j]=1;
            }
           else
            if (ch=='J')
            {

                lj=i;
                cj=j;
                h[i][j]=1;
            }
            else
            if(ch=='\n'&&j<=m)
            {   ok=0;
                 break;

            }


        }


    }

        OK=1;
        coada(lr,cr);
        OK=2;
        coada(lj,cj);
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                if(r[i][j]==h[i][j])
                    if(Min>r[i][j])
                    {
                        Min=r[i][j];
                        sol1=i;
                        sol2=j;
                    }
                    else
                        if(Min==r[i][j])
                        {
                            if(i<sol1)
                            {
                                Min=r[i][j];
                                sol1=i;
                                sol2=j;
                            }
                            else
                                if(i==sol1)
                                {
                                    if(j<sol2)
                                    {
                                        Min=r[i][j];
                                        sol1=i;
                                        sol2=j;
                                    }
                                }

                    }
     fout<<Min<<" "<<sol1<<" "<<sol2;

    return 0;
}