Cod sursa(job #3159388)

Utilizator BogaBossBogdan Iurian BogaBoss Data 21 octombrie 2023 11:10:08
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <bits/stdc++.h>
#include <fstream>
#include <iterator>
#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 secundar[110][110];
	int tertiar[110][110];
	position julietta;
	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});
				secundar[i][j] = 1;
			}
			else if(nums[i][j] == 'J') julietta={i,j}, tertiar[i][j] = 1;
			else if(nums[i][j] == 'X') secundar[i][j] = -1, tertiar[i][j] = -1;
			else secundar[i][j] = 0, tertiar[i][j] = 0;

		}
	}
	lee(secundar, n, m);
    v.push(julietta);
	lee(tertiar, n ,m);

    int tmin=INT_MAX;
    position rasp;

	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
		    if(secundar[i][j] == tertiar[i][j] && secundar[i][j]>0)
            {
                if(secundar[i][j] < tmin)
                {
                    tmin=secundar[i][j];
                    rasp={i,j};
                }
                else
                {
                    if(secundar[i][j] == tmin)
                    {
                        if(i<rasp.x)
                        {
                            rasp={i,j};
                        }
                        else if(i==rasp.x && j<rasp.y)
                            {
                                rasp={i,j};
                            }
                    }
                }
            }
		}
	}
    fout<<tmin<<" "<<rasp.x+1<<" "<<rasp.y+1;
}