Cod sursa(job #976066)

Utilizator manutrutaEmanuel Truta manutruta Data 22 iulie 2013 14:48:19
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.21 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];
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 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;
            }
            else {
                a[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;*/

    int solution = a[J.x][J.y] / 2 + 1;
    g << solution << ' ';

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (a[i][j] == solution) {
                g << i << ' ' << j + 1;
            }
        }
    }

    return 0;
}