Cod sursa(job #211783)

Utilizator brokensocialiconGrigoriu Cristian-Andrei brokensocialicon Data 3 octombrie 2008 17:09:52
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.64 kb
#include <iostream>
#include <stdio.h>
#include <string>
#include <queue>

using namespace std;
int n,m,x1,y1,x2,y2;
int a[175][175], r[175][175], ju[175][175];
struct coord {int x,y;};
queue<coord> coada;

void citire()
{
    char buffer[175];
    string c;
    int j;

    freopen("rj.in","r",stdin);
    scanf("%d %d",&n,&m);
    gets(buffer);
    for (int i=1; i<=n; i++)
    {
        gets(buffer);
        c=buffer;
        for (int j=0;j<c.length();j++)
        {
            if (c.substr(j,1)=="R")
            {
             a[i][j+1]=1;
             x1=i;
             y1=j+1;
            }
            if (c.substr(j,1)=="X")
             a[i][j+1]=3;
            if (c.substr(j,1)=="J")
            {
             a[i][j+1]=2;
             x2=i;
             y2=j+1;
            }
        }
    }
}

void fill_matrice(int t[][175],int xstart,int ystart)
{
    int auxx,auxy;
    coord nod;

    nod.x=xstart;
    nod.y=ystart;
    coada.push(nod);
    t[xstart][ystart]=1;
    while (!coada.empty())
    {
        auxx=coada.front().x;
        auxy=coada.front().y;
        coada.pop();
        if (auxy-1>=1 && a[auxx][auxy-1]==0 && t[auxx][auxy-1]==0)
        {
            t[auxx][auxy-1]=t[auxx][auxy]+1;
            nod.x=auxx;
            nod.y=auxy-1;
            coada.push(nod);
        }
        if (auxy+1<=m && a[auxx][auxy+1]==0 && t[auxx][auxy+1]==0)
        {
            t[auxx][auxy+1]=t[auxx][auxy]+1;
            nod.x=auxx;
            nod.y=auxy+1;
            coada.push(nod);
        }
        if (auxx-1>=1 && a[auxx-1][auxy]==0 && t[auxx-1][auxy]==0)
        {
            t[auxx-1][auxy]=t[auxx][auxy]+1;
            nod.x=auxx-1;
            nod.y=auxy;
            coada.push(nod);
        }
        if (auxx+1<=n && a[auxx+1][auxy]==0 && t[auxx+1][auxy]==0)
        {
            t[auxx+1][auxy]=t[auxx][auxy]+1;
            nod.x=auxx+1;
            nod.y=auxy;
            coada.push(nod);
        }
        if (auxy-1>=1 && auxx-1>=1 && a[auxx-1][auxy-1]==0 && t[auxx-1][auxy-1]==0)
        {
            t[auxx-1][auxy-1]=t[auxx][auxy]+1;
            nod.x=auxx-1;
            nod.y=auxy-1;
            coada.push(nod);
        }
        if (auxy+1<=m && auxx-1>=1 && a[auxx-1][auxy+1]==0 && t[auxx-1][auxy+1]==0)
        {
            t[auxx-1][auxy+1]=t[auxx][auxy]+1;
            nod.x=auxx-1;
            nod.y=auxy+1;
            coada.push(nod);
        }
        if (auxy-1>=1 && auxx+1<=n && a[auxx+1][auxy-1]==0 && t[auxx+1][auxy-1]==0)
        {
            t[auxx+1][auxy-1]=t[auxx][auxy]+1;
            nod.x=auxx+1;
            nod.y=auxy-1;
            coada.push(nod);
        }
        if (auxy+1<=m && auxx+1<=n && a[auxx+1][auxy+1]==0 && t[auxx+1][auxy+1]==0)
        {
            t[auxx+1][auxy+1]=t[auxx][auxy]+1;
            nod.x=auxx+1;
            nod.y=auxy+1;
            coada.push(nod);
        }
    }
}

int main()
{
    bool k=false;
    citire();
    fill_matrice(r,x1,y1);
    fill_matrice(ju,x2,y2);

    for (int j=1; j<=m;j++)
    {
     for (int i=1; i<=n;i++)
         if (r[i][j]==ju[i][j] and r[i][j]>0)
         {
            freopen ("rj.out","w",stdout);
            printf ("%d %d %d",i,j,r[i][j]);
            fclose (stdout);
            k=true;
            break;
         }
         if (k) break;
    }


    for (int i=1; i<=n; i++)
    {
     for (int j=1; j<=m; j++)
       cout << r[i][j] << " ";
    cout << "\n";
    }
    cout << "\n";
    for (int i=1; i<=n; i++)
    {
     for (int j=1; j<=m; j++)
       cout << ju[i][j] << " ";
    cout << "\n";
    }
    return 0;
}