Cod sursa(job #1344238)

Utilizator dumbraveanbDumbravean Bogdan dumbraveanb Data 16 februarie 2015 15:43:58
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream fin("rj.in");
ofstream fout("rj.out");

int A[101][101],B[101][101],a[10001],b[10001],n,m,is,js,ib,jb;
const int di[]={-1,0,1,0}, dj[]={0,1,0,-1};
char c[102];
int inside(int i,int j)
{
    return i>=1 && i<=n && j>=1 && j<=m;
}

int main()
{
    fin>>n>>m;
    for(int i=0;i<=n;i++)
    {
        fin.getline(c,102);
        for(int j=0;j<strlen(c);j++)
        {
            if(c[j]==' ') {A[i][j+1]=0; B[i][j+1]=0;}
            else if(c[j]=='X') {A[i][j+1]=-1; B[i][j+1]=-1;}
            else if(c[j]=='R') {A[i][j+1]=1; is=i; js=j; B[i][j+1]=1;}
            else if(c[j]=='J') {A[i][j+1]=1; ib=i; jb=j; B[i][j+1]=1;}
        }
    }
fout<<'\n';
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            fout<<A[i][j]<<' ';
        fout<<'\n';
    }
    fout<<'\n';
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            fout<<B[i][j]<<' ';
        fout<<'\n';
    }
    fout<<'\n';

    a[1]=is; b[1]=js; A[is][js]=1;
    int s=1,d=1;
    while (s<=d && !(a[s]==ib && b[s]==jb))
    {
        int i=a[s],j=b[s];
        for(int k=0;k<4;k++)
        {
            int inou=i+di[k], jnou=j+dj[k];
            if(inside(inou,jnou) && A[inou][jnou]==0)
            {
                A[inou][jnou]=A[i][j]+1;
                d++;
                a[d]=inou; b[d]=jnou;
            }
        }
        s++;
    }
    int aux1,aux2;
    a[1]=is; b[1]=js; B[is][js]=1;
    s=1,d=1;
    while (s<=d && !(a[s]==ib && b[s]==jb))
    {
        int i=a[s],j=b[s];
        for(int k=0;k<4;k++)
        {
            int inou=i+di[k], jnou=j+dj[k];
            if(inside(inou,jnou) && B[inou][jnou]==0)
            {
                B[inou][jnou]=B[i][j]+1;
                d++;
                a[d]=inou; b[d]=jnou;
            }
        }
        s++;
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            fout<<B[i][j]<<' ';
        fout<<'\n';
    }
    return 0;
}