Cod sursa(job #2315726)

Utilizator copanelTudor Roman copanel Data 10 ianuarie 2019 14:42:10
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
/* ( ‘_>’) */
#include <fstream>
#include <queue>
#include <tuple>
#include <utility>

using namespace std;

int romeo[102][102];
int julieta[102][102];
pair<int, int> dir[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

void bfs(int i, int j, int (&mat)[102][102]) {
	queue< pair<int, int> > q;
	q.push(make_pair(i, j));
	mat[i][j] = 0;
	while (!q.empty()) {
		int lin, col;
		tie(lin, col) = q.back();
		q.pop();
		for (auto d : dir) {
			int l = lin + d.first, c = col + d.second;
			if (mat[l][c] == -1) {
				q.push(make_pair(l, c));
				mat[l][c] = mat[lin][col] + 1;
			}
		}
	}
}

int main() {
	ifstream fin("rj.in");
	ofstream fout("rj.out");
	int n, m;
	int ir, jr, ij, jj;
	int x, y;

	fin >> n >> m;
	fin.get();
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			char c;
			fin >> noskipws >> c;
			if (c == ' ' || c == 'R' || c == 'J') {
				romeo[i][j] = julieta[i][j] = -1;
				if (c == 'R') {
					ir = i;
					jr = j;
				} else if (c == 'J') {
					ij = i;
					jj = j;
				}
			} else if (c == 'X') {
				romeo[i][j] = julieta[i][j] = -2;
			}
		}
		fin.get();
	}

	bfs(ir, jr, romeo);
	bfs(ij, jj, julieta);

	x = y = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (romeo[i][j] != julieta[i][j])
				continue;
			if (romeo[i][j] > 0 && (romeo[i][j] < romeo[x][y] || romeo[x][y] < 1)) {
				x = i;
				y = j;
			}
		}
	}

	fout << romeo[x][y] + 1 << ' ' << x << ' ' << y;

	return 0;
}