Cod sursa(job #2426771)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 29 mai 2019 11:26:19
Problema Castel Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <cstdio>
#include <queue>

using namespace std;

struct CASTEL {
	int x, y;
};

const int NMAX = 155;

CASTEL q[NMAX * NMAX];
bool viz[NMAX][NMAX];
vector<int>chei;
int a[NMAX][NMAX];

int n, m, k, p, u;
int xi, yi;
int dx[] = { 0,1,-1,0 };
int dy[] = { 1,0,0,-1 };

inline bool inMatrix(int& x, int& y)
{
	if (x >= 1 && x <= m && y >= 1 && y <= n)
		return true;
	return false;
}

void setCoordinates(int& x, int& y, int camera)
{
	if (camera % n == 0)
		x = camera / n;
	else
		x = camera / n + 1;
	if (camera % n == 0)
		y = n;
	else
		y = camera % n;
}

bool sePoateDeschide(CASTEL b)
{
	int xi, yi;
	setCoordinates(xi, yi, a[b.x][b.y]);
	if (viz[xi][yi] == 1)
		return true;
	return false;
}

void lee()
{
	while (1)
	{
		bool ok = false;
		CASTEL temp, temp1;
		for (int k = p; k <= u; k++)
		{
			temp = q[k];
			for (int i = 0; i < 4; i++)
			{
				temp1.x = temp.x + dx[i];
				temp1.y = temp.y + dy[i];
				if (!inMatrix(temp1.x, temp1.y))
					continue;
				if (viz[temp1.x][temp1.y] == 0 && sePoateDeschide(temp1))
				{
					viz[temp1.x][temp1.y] = 1;
					chei.push_back(a[temp1.x][temp1.y]);
					q[++u] = temp1;
					ok = true;
				}
			}
		}
		if (ok == false)
			return;
	}
}

int main()
{
	freopen("castel.in", "r", stdin);
	freopen("castel.out", "w", stdout);
	scanf("%d%d%d", &m, &n, &k);
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &a[i][j]);
	setCoordinates(xi, yi, k);
	p = 0;
	u = 1;
	CASTEL temp;
	temp.x = xi;
	temp.y = yi;
	q[++p] = temp;
	viz[xi][yi] = 1;
	chei.push_back(k);
	lee();
	int cnt = 0;
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= n; j++)
			if (viz[i][j])
				cnt++;
	printf("%d", cnt);
	return 0;
}