Pagini recente » Cod sursa (job #221802) | Cod sursa (job #2526493) | Cod sursa (job #514662) | Cod sursa (job #558677) | Cod sursa (job #519777)
Cod sursa(job #519777)
#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; ++i)
for (int j = 1; j <= M; ++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();
}