Cod sursa(job #933468)

Utilizator DarkyAngelDarky Angel DarkyAngel Data 29 martie 2013 23:56:37
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f("rj.in");
ofstream g("rj.out");

int n, m, xr, yr, xj, yj, hr[102][102], hj[102][102];
char h[102][102];
short dx[8] = {1, -1, 1, 0, -1,  1,  0, -1};
short dy[8] = {0,  0, 1, 1,  1, -1, -1, -1};

void lee (int x0, int y0, int d[102][102])
{
    int i, cost, pi, pf, x, y, Cl[102], Cc[102];
    bool viz[102][102];
    pi = pf = 0;
    d[x0][y0] = 1;
    viz[x0][y0] = 1;
    Cl[pf] = x0;
    Cc[pf] = y0;
    pf++;
    while(pi < pf)
    {
        for(i = 0; i < 8; i++)
        {
            x = Cl[pi] + dx[i];
            y = Cc[pi] + dy[i];
            cost = d[x-dx[i]][y-dy[i]] + 1;
            if( x >= 0 && x < n && y >= 0 && y < m && d[x][y] == 0 && h[x][y] == ' ')
            {
                Cl[pf] = x;
                Cc[pf] = y;
                pf++;
                viz[x][y] = 1;
                d[x][y] = cost;
            }
        }
        pi++;
    }
}

int main ()
{
    int i, j, lenc;
    char c[1001];
    f >> n >> m;
    f.get();
    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
            h[i][j] = ' ';
    for(i = 0; i < n; i++)
    {
        f.getline(c, 102);
        lenc = strlen(c);
        for(j = 0; j < lenc; j++)
        {
            h[i][j] = c[j];
            if(h[i][j] == 'R')
                xr = i, yr = j, h[i][j] = ' ';
            else if(h[i][j] == 'J')
                xj = i, yj = j, h[i][j] = ' ';
        }
    }
    f.close();

    lee(xj, yj, hj);
    lee(xr, yr, hr);

    int costmin = 10001, xmin, ymin;
    for(i = 0; i < n; i++)
        for (j = 0; j < m; j++)
        {
            if(hr[i][j] == hj[i][j] && hr[i][j] < costmin && hr[i][j] > 0)
            {
                costmin = hr[i][j];
                xmin    = i;
                ymin    = j;
            }
        }
    g << costmin << ' ' << xmin + 1 << ' ' << ymin + 1;
    g.close();
}