Cod sursa(job #2696688)

Utilizator NeoxDragos Stefan Neox Data 16 ianuarie 2021 13:21:36
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <utility>
#include <queue>
#include <vector>
#include <string>
using namespace std;

ifstream fin("rj.in");
ofstream fout("rj.out");
const int lim = 100;
int n, m, t_opt, i_opt, j_opt, tR[lim][lim], tJ[lim][lim];
string s[lim];
bool visR[lim][lim], visJ[lim][lim];
queue<pair<int, int>> R, J;
vector<pair<int, int>> dir = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
bool ok(int i, int j, bool vis[lim][lim]) {
	if(i >= 0 && i < n && j >= 0 && j < m && !vis[i][j] && s[i][j] != 'X') {
		return 1;
	}
	return 0;
}
void lee(queue<pair<int, int>> Q, bool vis[lim][lim], int t[lim][lim]) {
	while(!Q.empty()) {
		pair<int, int> coord = Q.front();
		Q.pop();
		int i_now = coord.first, j_now = coord.second;
		for(pair<int, int> p : dir) {
			int i_new = i_now + p.first, j_new = j_now + p.second;
			if(ok(i_new, j_new, vis)) {
				Q.push({i_new, j_new});
				vis[i_new][j_new] = 1;
				t[i_new][j_new] = t[i_now][j_now] + 1;
			}
		}
	}
}
int main() {
	fin >> n >> m;
	fin.ignore();
	for(int i = 0; i < n; i++) {
		getline(fin, s[i]);
		for(int j = 0; j < m; j++) {
			switch(s[i][j]) {
				case 'R':
					R.push({i, j});
					visR[i][j] = 1;
					break;
				case 'J':
					J.push({i, j});
					visJ[i][j] = 1;
			}
		}
	}
	lee(R, visR, tR);
	lee(J, visJ, tJ);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(tR[i][j] && tR[i][j] == tJ[i][j] && tR[i][j] > t_opt) {
				t_opt = tR[i][j];
				i_opt = i, j_opt = j;
			}
		}
	}
	fout << t_opt + 1 << ' ' << i_opt + 1 << ' ' << j_opt + 1;
	fin.close();
	fout.close();
	return 0;
}