Cod sursa(job #2335046)

Utilizator daru06Daria Culac daru06 Data 3 februarie 2019 15:53:18
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.06 kb
#include <fstream>
#include <cstring>
#include <climits>
using namespace std;

char S[101][101];
int R[105][105],J[105][105];
int x[10002],y[10002];
int a,b,c,d;
int diri[]={-1, 0 ,1, 0, -1, 1, 1, -1};
int dirj[]={0, 1 , 0, -1, 1, 1, -1, -1};
int tmin=INT_MAX,tt=0,t;
int main()
{
    ifstream f("rj.in");
    ofstream g("rj.out");

    int n,m;
    f>>n>>m;
    f.get();
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            f.get(S[i][j]);
            if(S[i][j]=='X')
                R[i][j]=J[i][j]=-1;
            else
                if(S[i][j]=='R')
                    a=i,b=j;
           else
                if(S[i][j]=='J')
                    c=i,d=j;
        }
        f.get();
    }
    int p,u;
    p=u=1;
    x[p]=a;
    y[p]=b;
    R[a][b]=1;
    while(p<=u)
    {
        int x1,y1;
        x1=x[p];
        y1=y[p];
        for(int d=0;d<8;d++)
        {
            int x2,y2;
            x2=x1+diri[d];
            y2=y1+dirj[d];
            if(x2>=1 and x2<=n and y2>=1 and y2<=m and R[x2][y2]==0)
            {
                R[x2][y2]=R[x1][y1]+1;
                u++;
                x[u]=x2;
                y[u]=y2;
            }
        }
        p++;
    }
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    p=u=1;
    x[p]=c;
    y[p]=d;
    J[c][d]=1;
    while(p<=u)
    {
        int x1,y1;
        x1=x[p];
        y1=y[p];
        for(int d=0;d<8;d++)
        {
            int x2,y2;
            x2=x1+diri[d];
            y2=y1+dirj[d];
            if(x2>=1 and x2<=n and y2>=1 and y2<=m and J[x2][y2]==0)
            {
                J[x2][y2]=J[x1][y1]+1;
                u++;
                x[u]=x2;
                y[u]=y2;
            }
        }
        p++;
    }

    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(R[i][j]==J[i][j] and R[i][j]>0 and R[i][j]<tmin)
    {
        tmin=R[i][j];
        t=i;
        tt=j;
    }
    g<<tmin<<" "<<t<<" "<<tt;
    f.close();
    g.close();
    return 0;
}