Pagini recente » Cod sursa (job #2304807) | Cod sursa (job #1279252) | Cod sursa (job #844595) | Statistici sabina (sabinuta) | Cod sursa (job #2791246)
#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++) {
string s;
getline(fin, s);
int len = s.size();
for (int j = 0; j < len; 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 lee(int cx, int cy, int mat[NMAX][NMAX]) {
mat[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 && mat[xx][yy] == 0) {
mat[xx][yy] = mat[x][y] + 1;
que.push({ xx,yy });
}
}
}
}
}
int main()
{
fin >> n >> m;
citire();
lee(xr,yr, rlee);
lee(xj, yj, jlee);
int fx = 0;
int fy = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m+1; 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;
}
}
}
fout << minc << ' ' << fx << ' ' << fy;
return 0;
}