Cod sursa(job #2791223)

Utilizator HadircaDionisieHadirca Dionisie HadircaDionisie Data 30 octombrie 2021 11:22:58
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>	
#include <algorithm>

#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 vectorul de distante
int minc = INT_MAX;

char matrice[NMAX][NMAX] = {'0'};

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);


		for (int j = 0; j < s.size(); j++) {
			matrice[i][j + 1] = s[j];
			if (s[j] == 'R') {
				xr = i;
				yr = j + 1;
			}
			else if (s[j] == 'J') {
				xj = i;
				yj = j + 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] == ' ' || matrice[xx][yy] == 'J' || matrice[xx][yy] == 'R') && 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, fy;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (rlee[i][j] == jlee[i][j]) {
				int x = rlee[i][j];
				if (x!=0 && x < minc) {
					fx = i;
					fy = j;
					minc = x;
				}
			}
		}
	}
	
	fout << minc << ' ' << fx << ' ' << fy;
	return 0;
}