Cod sursa(job #2155768)

Utilizator HelloWorldBogdan Rizescu HelloWorld Data 8 martie 2018 09:05:29
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
int i,j,x,xx,y,yy,n,m,b[100][100],c[100][100],ii,jj,minn=499999,cox,coy;
int dx[]= {1,1,0,-1,-1,-1,0,1};
int dy[]= {0,1,1,1,0,-1,-1,-1};
char a[100][100];
queue <pair <int,int> > coada;
bool ok(int x,int y)
{
    if (x<1 || y<1 || x>n || y>m) return 0;
    return 1;
}
int main()
{
    in>>n>>m;
    in.get();
    for (i=1; i<=n; ++i)
    {
        for (j=1; j<=m; ++j)
        {
            in.get(a[i][j]);
            if (a[i][j]=='R') x=i,y=j,b[i][j]=1,c[i][j]=1;
            else if (a[i][j]=='J') xx=i,yy=j,b[i][j]=1,c[i][j]=1;
            else if (a[i][j]=='X') b[i][j]=-1,c[i][j]=-1;
            else if (a[i][j]==' ') b[i][j]=0,c[i][j]=0;
        }
        in.get();
    }
    coada.push(make_pair(x,y));
    while (!coada.empty())
    {
        x=coada.front().first;
        y=coada.front().second;
        coada.pop();
        for (int q=0; q<8; ++q)
        {
            ii=x+dx[q];
            jj=y+dy[q];
            if (ok(ii,jj) && !b[ii][jj])
            {
                b[ii][jj]=b[x][y]+1;
                coada.push(make_pair(ii,jj));
            }
        }
    }
    coada.push(make_pair(xx,yy));
    while (!coada.empty())
    {
        x=coada.front().first;
        y=coada.front().second;
        coada.pop();
        for (int q=0; q<8; ++q)
        {
            ii=x+dx[q];
            jj=y+dy[q];
            if (ok(ii,jj) && !c[ii][jj])
            {
                c[ii][jj]=c[x][y]+1;
                coada.push(make_pair(ii,jj));
            }
        }
    }
    for (i=1; i<=n; ++i)
    {
        for (j=1; j<=m; ++j)
            if (b[i][j]==c[i][j] && b[i][j]!=-1 && b[i][j]!=1 && b[i][j]!=0)
            {
                if (b[i][j]<minn)
                {
                    minn=b[i][j];
                    cox=i;
                    coy=j;
                }
            }
    }
    out<<minn<<" "<<cox<<" "<<coy<<"\n";
}