Pagini recente » Cod sursa (job #2660448) | Cod sursa (job #730952) | Cod sursa (job #1712286) | Cod sursa (job #1929834) | Cod sursa (job #1490632)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int camera[155][155];
bool gasit[155 * 155];
vector<int> deexplorat[155 * 155];
int n, m;
void rez(int k)
{
vector<int> coada;
ofstream out("castel.out");
int i,x,y,j,t=0;
coada.push_back(k);
for (i = 0; i < coada.size(); i++)
{
x = coada[i];
y = x%m;
x = x / m;
if (gasit[x*m + y] == 0){ t++; }
else{ continue; }
gasit[x*m + y] = 1;
for (j = 0; j < deexplorat[x*m + y].size(); j++)
{
coada.push_back(deexplorat[x*m + y][j]);
}
if (x>0 && gasit[(x - 1)*m + y] == 0)
{
if (gasit[camera[x - 1][y]] == 1)
{
coada.push_back((x - 1)*m + y);
}
else
{
deexplorat[camera[x - 1][y]].push_back((x - 1)*m + y);
}
}
if (x<n-1 && gasit[(x + 1)*m + y] == 0)
{
if (gasit[camera[x + 1][y]] == 1)
{
coada.push_back((x + 1)*m + y);
}
else
{
deexplorat[camera[x + 1][y]].push_back((x + 1)*m + y);
}
}
if (y > 0 && gasit[x*m + y - 1] == 0)
{
if (gasit[camera[x][y - 1]] == 1)
{
coada.push_back(x*m + y - 1);
}
else
{
deexplorat[camera[x][y - 1]].push_back(x*m + y - 1);
}
}
if (y < m-1 && gasit[x*m + y + 1] == 0)
{
if (gasit[camera[x][y + 1]] == 1)
{
coada.push_back(x*m + y + 1);
}
else
{
deexplorat[camera[x][y + 1]].push_back(x*m + y + 1);
}
}
}
out << t;
}
int main()
{
ifstream in("castel.in");
int i, j, k;
in >> n;
in >> m;
in >> k;
k--;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
in >> camera[i][j];
camera[i][j]--;
}
}
rez(k);
}