Pagini recente » Cod sursa (job #1420982) | Cod sursa (job #1509975) | Cod sursa (job #2652218) | Cod sursa (job #2349475) | Cod sursa (job #3344659)
//#pragma GCC optimize("O3, Ofast, unroll-loops")
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int nm=155;
int mat[nm][nm];
bool ver[nm][nm];
int chei[nm*nm];
int d1[]={-1, 1, 0, 0};
int d2[]={0, 0, -1, 1};
int n, m, cnt=1;
void bfs(int x, int y)
{
chei[(x-1)*m+y]=1;
queue<pair<int, int>> q;
q.push({x, y});
ver[x][y]=1;
while(1)
{
int ok=0;
map<pair<int, int>, bool> mp;
while(!q.empty())
{
x=q.front().first;
y=q.front().second;
q.pop();
for(int i=0; i<4; i++)
{
int nx=x+d1[i], ny=y+d2[i];
if(nx>0 && nx<=n && ny>0 && ny<=m && ver[nx][ny]==0)
{
if(chei[mat[nx][ny]]==1)
{
ok=1;
chei[(nx-1)*m+ny]=1;
cnt++;
ver[nx][ny]=1;
q.push({nx, ny});
}
else
mp[{x, y}]=1;
}
}
}
if(ok==0)
break;
for(auto it:mp)
q.push({it.first.first, it.first.second});
}
}
int main()
{
ifstream cin("castel.in");
ofstream cout("castel.out");
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int k;
cin>>n>>m>>k;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
cin>>mat[i][j];
}
}
if(k%m==0)
bfs(k/m, m);
else
bfs(k/m+1, k%m);
cout<<cnt;
return 0;
}