Pagini recente » Cod sursa (job #1758496) | Cod sursa (job #436657) | Cod sursa (job #317906) | Cod sursa (job #1205134) | Cod sursa (job #1666513)
#include <fstream>
#define mod 20000
#define Mod 200
#define INF 0x7fffffff
#define pune_in_coada(xx,yy) coada[0][(++dr)%mod] = xx, coada[1][dr%mod] = yy;
#define pune_in_alta_coada(keie,xx,yy) ramas[keie][(D[keie]++)%Mod].x = xx,ramas[keie][(D[keie])%Mod].y = yy
#define pune_dintr_o_coada_in_alta(keie) coada[0][++dr] = ramas[keie][(S[keie])%Mod].x, coada[1][dr] = ramas[keie][(S[keie]++)%Mod].y
using namespace std;
ifstream f("castel.in");
ofstream g("castel.out");
int n, m, i, j, k, a[151][151], xi, yi;
int b[151][151];
bool cheie[22555];
struct
{
int x, y;
}ramas[152][Mod];
int S[152], D[152];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
bool inauntru(int x, int y)
{
return (x >= 1 && x <= n && y >= 1 && y <= m);
}
void cauta()
{
int coada[2][mod], st = 0, dr = 0;
int x, y, xx, yy;
coada[0][st] = xi, coada[1][st] = yi;
b[xi][yi] = 0;
cheie[(xi-1)*m+yi] = 1;
while (st <= dr)
{
x = coada[0][st%mod], y = coada[1][st%mod];
for (i = 0; i < 4; i++)
{
xx = x+dx[i], yy = y+dy[i];
if (inauntru(xx, yy))
{
if (cheie[a[xx][yy]] == 1 && b[xx][yy] > b[x][y]+1)
{
pune_in_coada(xx,yy);
b[xx][yy] = b[x][y]+1;
cheie[(xx-1)*m + yy] = 1;
while (S[a[xx][yy]] <= D[a[xx][yy]])
pune_dintr_o_coada_in_alta(a[xx][yy]);
}
else;
pune_in_alta_coada(a[xx][yy], xx, yy);
}
}
st++;
}
}
int main()
{
f >> n >> m >> k;
xi = k/m+(k%m>0), yi = k%m;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
f >> a[i][j], b[i][j] = INF;
cauta();
int cnt = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (b[i][j] != INF)
cnt++;
g << cnt;
return 0;
}