Cod sursa(job #3155547)

Utilizator stefanchpStefan Chiper stefanchp Data 8 octombrie 2023 16:11:29
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

ifstream fin("rj.in");
ofstream fout("rj.out");

char s[101];
int n, m, R[105][105], J[105][105];
int dl[] = { 1,1,-1,-1,0,0,1,-1 };
int dc[] = { 1,-1,1,-1,1,-1,0,0 };
queue<pair<int, int>> q1, q2;

void citire()
{
	fin >> n >> m;
	fin.get();
	for (int i = 1; i <= n; i++)
	{
		fin.getline(s, 101);
		for (int j = 0; j < m; j++)
			if (s[j] == 'X')
				R[i][j + 1] = J[i][j + 1] = -1;
			else if (s[j] == 'R')
				R[i][j + 1] = 1, q1.push({ i,j + 1 });
			else if (s[j] == 'J')
				J[i][j + 1] = 1, q2.push({ i,j + 1 });
	}
	fin.close();
}

inline bool inmat(int l, int c)
{
	if (1 <= l && l <= n && 1 <= c && c <= m)
		return 1;
	return 0;
}

void lee(queue<pair<int, int>>q, int a[][105])
{
	int lv, cv;
	pair<int, int> aux;
	while (!q.empty())
	{
		aux = q.front();
		q.pop();
		for (int i = 0; i < 8; i++)
		{
			lv = aux.first + dl[i];
			cv = aux.second + dc[i];
			if (inmat(lv,cv) && a[lv][cv] == 0)
			{
				a[lv][cv] = a[aux.first][aux.second] + 1;
				q.push({ lv, cv });
			}
		}
	}
}

void solutie()
{
	int linie = 0, coloana = 0, minim = n * m + 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (R[i][j] > 0 && R[i][j] == J[i][j] && R[i][j] < minim)
			{
				minim = R[i][j];
				linie = i;
				coloana = j;
			}
	fout << minim << ' ' << linie << ' ' << coloana;
	fout.close();
}

int main()
{
	citire();
	lee(q1,R);
	lee(q2,J);
	solutie();
	return 0;
}