Cod sursa(job #519779)

Utilizator darrenRares Buhai darren Data 6 ianuarie 2011 15:33:09
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <fstream>
#include <utility>

using namespace std;

const int D1[] = {0, 1, 0, -1},
          D2[] = {1, 0, -1, 0};
		  
int N, M, K;
int V[152][152], enter = 1;
bool Key[22502], S[152][152];

int getCod(int x, int y)
{
	return (x - 1) * M + y;
}
pair<int, int> getCoord(int cod)
{
	int x = (cod % M == 0 ? cod / M : cod / M + 1);
	cod -= (x - 1) * M;
	return make_pair(x, cod);
}

int main()
{
	ifstream fin("castel.in");
	ofstream fout("castel.out");
	
	fin >> N >> M >> K;
	for (int i = 1; i <= N; ++i)
		for (int j = 1; j <= M; ++j)
			fin >> V[i][j];
	
	pair<int, int> coord = getCoord(K);
	S[coord.first][coord.second] = true;
	Key[K] = true;
	
	bool ok = true;
	while (ok)
	{
		ok = false;
		for (int i = 1; i <= N && !ok; ++i)
			for (int j = 1; j <= M && !ok; ++j)
				if (S[i][j] == true)
					for (int k = 0; k < 4; ++k)
					{
						int nowx = i + D1[k], nowy = j + D2[k];
						if (nowx >= 1 && nowx <= N && nowy >= 1 && nowy <= M)
							if (S[nowx][nowy] == false && Key[V[nowx][nowy]] == true)
							{
								Key[getCod(nowx, nowy)] = true, S[nowx][nowy] = true;
								ok = true;
								++enter;
							}
					}
	}
	
	fout << enter;
	
	fin.close();
	fout.close();
}