Cod sursa(job #88938)

Utilizator Adriana_SAdriana Sperlea Adriana_S Data 4 octombrie 2007 21:23:15
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <stdio.h>
#include <vector>

using namespace std;

const int N_MAX = 156;

int M, N, K;
pair <int, int> a[N_MAX][N_MAX];
int viz[N_MAX][N_MAX], ch[N_MAX * N_MAX];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int viz_vec(int x, int y)
{
	int xx, yy, i;
	for (i = 0; i < 4; i ++) {
		xx = x + dx[i];
		yy = y + dy[i];

		if (viz[xx][yy]) return 1;
	}

	return 0;
}

int main()
{
	freopen("castel.in", "r", stdin);
#ifndef _SCREEN_
	freopen("castel.out", "w", stdout);
#endif

	int x, y, i, j;

	scanf("%d %d %d\n", &M, &N, &K);
	for (i = 1; i <= M; i ++) {
		for (j = 1; j <= N; j ++) {
			scanf("%d ", &x);

			a[i][j] = make_pair(x, (i - 1) * N + j);
		}
	}
	
	x = K / N + 1, y = K % N;
	if (y == 0) {
		x --;
		y = N;
	}
	viz[x][y] = 1;
	ch[K] = 1;

	int g = 1;
	while (g) {
		g = 0;

		for (i = 1; i <= M; i ++) {
			for (j = 1; j <= N; j ++) {
				if (!viz[i][j] && ch[a[i][j].first] && viz_vec(i, j)) {
					viz[i][j] = 1;
					ch[a[i][j].second] = 1;
					g = 1;
				}
			}
		}
	}

	int nr = 0;
	for (i = 1; i <= M; i ++) {
		for (j = 1; j <= N; j ++) {
			if (viz[i][j]) nr ++;
		}
	}

	printf("%d\n", nr);

	return 0;
}