Cod sursa(job #3159391)

Utilizator DARIUSQSDarius Nicoara DARIUSQS Data 21 octombrie 2023 11:15:38
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <bits/stdc++.h>
#include <fstream>
#include <iterator>
#include <memory>
#include <queue>
using namespace std;

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

struct position
{
	int x, y;
};

int di[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dj[] = {0, -1, -1, -1, 0, 1, 1, 1};

queue<position> v;

bool inmat(position pos, int n, int m)
{
	return (pos.x >= 0 && pos.x < n && pos.y >= 0 && pos.y < m);
}

void lee(int nums[110][110], int n, int m)
{
	while(!v.empty())
	{
		position last_position = v.front();
		for(int k = 0; k < 8; k++)
		{
			position new_position = {last_position.x + di[k], last_position.y + dj[k]};
			if(inmat(new_position, n, m) && nums[new_position.x][new_position.y] == 0)
			{
				v.push(new_position);
				nums[new_position.x][new_position.y] +=  nums[last_position.x][last_position.y] + 1;
			}
		}
		v.pop();
	}
}

int main()
{
	int n, m;
	fin >> n >> m;
	char nums[110][110];
	int R[110][110];
	int J[110][110];
	fin.get();
	for(int i = 0; i < n; i++)
	{
		fin.getline(nums[i], 101);
	}
	
	for(int i = 0;  i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			if(nums[i][j] == 'R') 
			{
				v.push({i, j});
				R[i][j] = 1;
			}
			else if(nums[i][j] == 'X') R[i][j] = -1;
			else R[i][j] = 0;
			
		}
	}
	lee(R, n, m);

	for(int i = 0;  i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			if(nums[i][j] == 'J') 
			{
				v.push({i, j});
				J[i][j] = 1;
			}
			else if(nums[i][j] == 'X') J[i][j] = -1;
			else J[i][j] = 0;
			
		}
	}
	lee(J, n, m);

/* 	for(int i = 0; i < n; i++) */
/* 	{ */
/* 		for(int j = 0; j < m; j++) */
/* 		{ */
/* 			fout << R[i][j] << ' '; */
/* 		} */
/* 		fout << '\n'; */
/* 	} */
	
/* 	fout << endl << endl; */

/* 	for(int i = 0; i < n; i++) */
/* 	{ */
/* 		for(int j = 0; j < m; j++) */
/* 		{ */
/* 			fout << J[i][j] << ' '; */
/* 		} */
/* 		fout << '\n'; */ 
/* 	} */

	int nmin = INT_MAX;
	position position_of_intersection;


	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			if(R[i][j] == J[i][j] && R[i][j] < nmin && R[i][j] > 0)
			{
				nmin = R[i][j];
				position_of_intersection = {i, j};
			}
		}
	}
	fout << R[position_of_intersection.x][position_of_intersection.y] << ' ' << position_of_intersection.x + 1 << ' ' << position_of_intersection.y + 1; 
}