Cod sursa(job #1586869)

Utilizator Hilbert_DennisHilbert Dennis Hilbert_Dennis Data 1 februarie 2016 18:10:46
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <iomanip>
#include <string>
using namespace std;

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

int n, m, i, j, ok, aux, nr, c[176][176], d[176][176], x1, y1, x2, y2, x, y, timp, p;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
string a;
struct elem {
    int x; int y;
} e, r;
queue <elem> q;

void prelucrez() {
    int k;
    r.x = x1; r.y = y1; c[r.x][r.y] = 1;
    q.push(r);
    while (!q.empty()) {
        e.x = q.front().x; e.y = q.front().y;
        q.pop();
        for (k = 0; k < 4; k++) {
            r.x = e.x + dx[k]; r.y = e.y + dy[k];
            if ((c[r.x][r.y] != -1) and (c[r.x][r.y] > c[e.x][e.y] + 1)) {
                    c[r.x][r.y] = c[e.x][e.y] + 1;
                    q.push(r);
            }
        }
    }
}
void prelucrez1() {
    int k;
    r.x = x2; r.y = y2; d[r.x][r.y] = 1;
    q.push(r);
    while (!q.empty()) {
        e.x = q.front().x; e.y = q.front().y;
        q.pop();
        for (k = 0; k < 4; k++) {
            r.x = e.x + dx[k]; r.y = e.y + dy[k];
            if ((d[r.x][r.y] != -1) and (d[r.x][r.y] > d[e.x][e.y] + 1)) {
                    d[r.x][r.y] = d[e.x][e.y] + 1;
                    q.push(r);
            }
        }
    }
}
int main() {
    fin >> n >> m;
    getline(fin, a);
    for(i = 1; i <= n; i++){
        getline(fin, a);
        for(j = 0; j < m; j++) {
            if(a[j] == 'R') {
                c[i][j + 1] = 2000;
                d[i][j + 1] = 2000;
                x1 = i;
                y1 = j + 1;
            }
            if(a[j] == 'J') {
                c[i][j + 1] = 2000;
                d[i][j + 1] = 2000;
                x2 = i;
                y2 = j + 1;
            }
            if(a[j] == 'X') {
                c[i][j + 1] = -1;
                d[i][j + 1] = -1;
            }

            if(a[j] == ' ') {
                c[i][j + 1] = 2000;
                d[i][j + 1] = 2000;
            }
        }
    }

    for(i = 0; i <= n + 1; i++) {
        c[i][0] = -1;
        c[i][m + 1] = -1;
        d[i][0] = -1;
        d[i][m + 1] = -1;
    }
    for(j = 0; j <= m + 1; j++) {
        c[0][j] = -1;
        c[n + 1][j] = -1;
        d[0][j] = -1;
        d[n + 1][j] = -1;
    }

    prelucrez();
    timp = (c[x2][y2] - 1) / 2;
    p = timp + 1;
    prelucrez1();
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= m; j++)
            if(c[i][j] == p and d[i][j] == p) {
                x = i;
                y = j;
            }
    }
    fout << timp << " " << x << " " << y;

return 0;
}