Pagini recente » Cod sursa (job #2689388) | Cod sursa (job #2269700) | Cod sursa (job #197326) | Cod sursa (job #494581) | Cod sursa (job #2315753)
/* ( ‘_>’) */
#include <fstream>
#include <queue>
#include <tuple>
#include <utility>
using namespace std;
int romeo[102][102];
int julieta[102][102];
pair<int, int> dir[] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {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.front();
q.pop();
for (int d = 0; d < 8; d++) {
int l = lin + dir[d].first, c = col + dir[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;
}