Pagini recente » Profil viorel123 | Cod sursa (job #1520876) | Cod sursa (job #81146) | Cod sursa (job #245053) | Cod sursa (job #2791314)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#define NMAX 105
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int n, m;
int xr, yr, xj, yj; //coordonatele lui romeo si a julietei in pentru matricea de distante
int minc = INT_MAX;
int matrice[NMAX][NMAX];
int dx[] = { 0, -1, 0, 1, -1, 1, -1, 1 };
int dy[] = { -1, 0, 1, 0, 1, -1, -1, 1 };
//matrici de distante
int rlee[NMAX][NMAX];
int jlee[NMAX][NMAX];
void citire() {
fin.get();
for (int i = 1; i < n + 2; i++) {
char s[105];
fin.getline(s, m + 1);
for (int j = 0; s[j]; j++) {
if (s[j] == 'R') {
xr = i;
yr = j + 1;
}
else if (s[j] == 'J') {
xj = i;
yj = j + 1;
}
else if (s[j] == 'X') {
matrice[i][j + 1] = 1;
}
}
}
}
void leer(int cx, int cy) {
rlee[cx][cy] = 1;
queue<pair<int, int>> que;
que.push({ cx,cy });
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 8; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 1 && yy >= 1 && xx <= n && yy <= n){
if (matrice[xx][yy] == 0 && rlee[xx][yy] == 0) {
rlee[xx][yy] = rlee[x][y] + 1;
que.push({ xx,yy });
}
}
}
}
}
void leej(int cx, int cy) {
jlee[cx][cy] = 1;
queue<pair<int, int>> que;
que.push({ cx,cy });
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 8; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 1 && yy >= 1 && xx <= n && yy <= n) {
if (matrice[xx][yy] == 0 && jlee[xx][yy] == 0) {
jlee[xx][yy] = jlee[x][y] + 1;
que.push({ xx,yy });
}
}
}
}
}
int main()
{
fin >> n >> m;
citire();
leer(xr,yr);
leej(xj, yj);
int fx = 0;
int fy = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (rlee[i][j] > 0 && jlee[i][j] > 0 && rlee[i][j] == jlee[i][j] && minc > rlee[i][j]) {
minc = rlee[i][j];
fx = i;
fy = j;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (rlee[i][j] > 0 && jlee[i][j] > 0 && rlee[i][j] == jlee[i][j] && (i<fx || j<fy)) {
if (minc >= rlee[i][j]) {
minc = rlee[i][j];
fx = i;
fy = j;
}
}
}
}
fout << minc << ' ' << fx << ' ' << fy;
return 0;
}