Cod sursa(job #2156047)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 8 martie 2018 13:56:21
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
queue <pair<int, int> > coada;
const int NMAX = 105;
int a[NMAX][NMAX], b[NMAX][NMAX], n, m;
struct {
	int x, y;
}directie[8] = { { -1,0 },{ 0,1 },{ 1,0 },{ 0,-1 },{ -1,-1 },{ -1,1 },{ 1, 1 },{ 1, -1 } };
int OK(int i, int j)
{
	if (i<1 || i>n || j<1 || j>m)
		return 0;
	if (b[i][j])
		return 0;
	return 1;
}
int OK2(int i, int j)
{
	if (i<1 || i>n || j<1 || j>m)
		return 0;
	if (a[i][j] == 0)
		return 0;
	return 1;
}
void lee(int x, int y)
{
	int new_x, new_y;
	b[x][y] = 1;
	coada.push(make_pair(x, y));
	int ok = 1;
	while (ok)
	{
		x = coada.front().first;
		y = coada.front().second;
		coada.pop();
		for (int i = 0; i < 8 && ok; i++)
		{
			new_x = x + directie[i].x;
			new_y = y + directie[i].y;
			if (OK(new_x, new_y) && a[new_x][new_y])
			{
				coada.push(make_pair(new_x, new_y));
				b[new_x][new_y] = b[x][y] + 1;
			}
			if (a[new_x][new_y] == 2)
				ok = 0;
		}
	}
	int t = b[new_x][new_y] - 1;
	x = new_x;
	y = new_y;
	g << t / 2 + 1 << " ";
	t = t / 2;
	while (t)
	{
		t--;
		int ok = 1;
		for (int i = 0; i < 8 && ok; i++)
		{
			new_x = x + directie[i].x;
			new_y = y + directie[i].y;
			if (b[new_x][new_y] == b[x][y] - 1 && OK2(new_x, new_y))
			{
				x = new_x;
				y = new_y;
				ok = 0;
			}
		}
	}
	g << x << " " << y << "\n";
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
			g << b[i][j] << " ";
		g << "\n";
	}
}
int val(char c)
{
	if (c == 'X')
		return 0;
	if (c == ' ' || c == 'A')
		return 1;
	if (c == 'R')
		return 2;
	return 3;
}
int main()
{
	f >> n >> m;
	int lin, col;
	f.get();
	for (int i = 1; i <= n; i++)
	{
		char c[101];
		f.getline(c, 101);
		if (strlen(c) == m - 1)
			strcat(c, "A");
		for (int j = 0; j < strlen(c); j++)
		{
			a[i][j + 1] = val(c[j]);
			if (a[i][j + 1] == 3)
			{
				lin = i;
				col = j + 1;
			}
		}
	}
	lee(lin, col);
	return 0;
}