Cod sursa(job #976076)

Utilizator manutrutaEmanuel Truta manutruta Data 22 iulie 2013 15:03:46
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.23 kb
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
using namespace std;

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

struct punct{
    int x, y;
};

string aux;
int a[110][110], b[110][110];
int n, m;
punct R, J;
punct directii[8] = {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
queue <punct> coada;

int isgut(punct p) {
    if (p.x < 1 || p.x > n)
        return 0;
    if (p.y < 0 || p.y >= m)
        return 0;
    if(a[p.x][p.y] == 0)
        return 1;
    return 0;
}

int isgut2(punct p) {
    if (p.x < 1 || p.x > n)
        return 0;
    if (p.y < 0 || p.y >= m)
        return 0;
    if(b[p.x][p.y] == 0)
        return 1;
    return 0;
}

int main()
{
    f >> n >> m;
    getline(f, aux);
    for (int i = 1; i <= n; i++) {
        getline(f, aux);
        for (int j = 0; j < m; j++) {
            if (aux[j] == 'R') {
                R.x = i;
                R.y = j;
            }
            if (aux[j] == 'J') {
                J.x = i;
                J.y = j;
            }
            if (aux[j] == 'X') {
                a[i][j] = -1;
                b[i][j] = -1;
            }
            else {
                a[i][j] = 0;
                b[i][j] = 0;
            }
        }
    }

    /*for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
    cout << R.x << ' ' << R.y << endl;
    cout << J.x << ' ' << J.y << endl;*/

    coada.push(R);
    a[R.x][R.y] = 1;
    while (coada.size()) {
        punct aux1;
        aux1 = coada.front();
        coada.pop();
        for (int i = 0; i < 8; i++) {
            punct aux2;
            aux2.x = aux1.x + directii[i].x;
            aux2.y = aux1.y + directii[i].y;
            if (isgut(aux2)) {
                a[aux2.x][aux2.y] = a[aux1.x][aux1.y] + 1;
                coada.push(aux2);
            }
        }
    }

    /*cout << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;*/

    coada.push(J);
    b[J.x][J.y] = 1;
    while (coada.size()) {
        punct aux1;
        aux1 = coada.front();
        coada.pop();
        for (int i = 0; i < 8; i++) {
            punct aux2;
            aux2.x = aux1.x + directii[i].x;
            aux2.y = aux1.y + directii[i].y;
            if (isgut2(aux2)) {
                b[aux2.x][aux2.y] = b[aux1.x][aux1.y] + 1;
                coada.push(aux2);
            }
        }
    }

    /*cout << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
            cout << b[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;*/

    int costmin = 100010, xmin = n + 1, ymin = m + 1;

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] == b[i][j] && a[i][j] > 0 && a[i][j] < costmin) {
                costmin = a[i][j];
                xmin = i;
                ymin = j + 1;
            }
        }
    }

    g << costmin << ' ' << xmin << ' ' << ymin;

    return 0;
}