Pagini recente » Cod sursa (job #1595967) | Cod sursa (job #1651743) | Cod sursa (job #113411) | Cod sursa (job #1127331) | Cod sursa (job #88938)
Cod sursa(job #88938)
#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;
}