Pagini recente » Cod sursa (job #2741127) | Cod sursa (job #1552462) | Cod sursa (job #2423077) | Cod sursa (job #1393680) | Cod sursa (job #3001073)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("castel.in");
ofstream fout("castel.out");
int n, m, k;
bool chei[23000];
int cam[152][152];
int lee[152][152];
struct pos
{
int x, y;
};
queue<pos> leePos;
vector<pos> close[23000];
int poz[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
void getChei(int cheie)
{
chei[cheie] = 1;
int cheie2;
for (int i = 0; i < close[cheie].size(); i++)
{
cheie2 = (close[cheie][i].x - 1) * m + close[cheie][i].y;
leePos.push(close[cheie][i]);
getChei(cheie2);
}
}
void solve()
{
while (!leePos.empty())
{
pos primu = leePos.front();
lee[primu.x][primu.y] = -1;
leePos.pop();
for (int i = 0; i < 4; i++)
{
int x, y;
x = primu.x + poz[i][0];
y = primu.y + poz[i][1];
if (x < 1 || y < 1 || x > n || y > m) continue;
int cheie = (x - 1) * m + y;
if (lee[x][y] == 0 && chei[cam[x][y]])
{
leePos.push({x, y});
getChei(cheie);
}
else if (lee[x][y] == 0)
{
close[cam[x][y]].push_back({x, y});
}
}
}
}
int main()
{
fin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
fin >> cam[i][j];
}
}
int x = (k - 1) / m + 1;
int y = (k - 1) % m + 1;
cout << x << ' ' << y;
leePos.push({x, y});
lee[x][y] = -1;
chei[k] = 1;
solve();
int cnt = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (lee[i][j] == 0)
{
fout << ' ';
}
fout << lee[i][j] << ' ';
if (lee[i][j] == -1)
cnt++;
}
fout << '\n';
}
fout << cnt;
return 0;
}