Cod sursa(job #683053)

Utilizator AllenSmailovic Alen Allen Data 19 februarie 2012 21:38:59
Problema Pascal Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

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

int a[100][100];
int n,m,xi,yi,xf,yf;
int u=1,p=0;
int di[8]={0,1,1,1,0,-1,-1,-1};
int dj[8]={1,1,0,-1,-1,-1,0,1};

struct coada{
    int x;
    int y;
}c[10000];

void citire()
{
    char x[101];
    fin>>n>>m;
    for (int i=0;i<=n;i++)
    {
        fin.getline(x,101);
        for (int j=0;j<strlen(x);j++)
        {
            if (x[j]=='X')
                a[i][j+1]=1;
            else
            {
                if (x[j]=='R')
                {
                    xi=i;
                    yi=j+1;
                }
                if (x[j]=='J')
                {
                    xf=i;
                    yf=j+1;
                }
            }
        }
    }
}

void bordare()
{
    for (int i=0;i<=n+1;i++)
    {
        a[i][0]=1;
        a[i][m+1]=1;
    }
    for (int i=0;i<=m+1;i++)
    {
        a[0][i]=1;
        a[n+1][i]=1;
    }
}

int lee()
{
    c[p].x=xi;
    c[p].y=yi;
    a[xi][yi]=1;
    while (p<u)
    {
        int i1=c[p].x;
        int j1=c[p].y;
        for (int k=0;k<8;k++)
        {
            int i2=i1+di[k];
            int j2=j1+dj[k];
            if (a[i2][j2]==0)
            {
                c[u].x=i2;
                c[u].y=j2;
                a[i2][j2]=a[i1][j1]+1;
                if (i2==xf && j2==yf)
                    return a[i2][j2];
                u++;
            }
        }
        p++;
    }
    return -1;
}

int main()
{
    citire();
    bordare();
    int pas=lee();
    cout<<pas<<" "<<c[pas].x
    <<" "<<c[pas].y;

    return 0;
}