Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e3+3;
- const int inf = 1e9+5;
- int tab[maxn][maxn], ob[maxn][maxn];
- bool odw[maxn][maxn];
- int a[4]={1,0,-1,0};
- int b[4]={0,1,0,-1};
- queue<pair<int,int> > q;
- int bfs(int k, int l){
- q.push({k,l});
- int suma = 0;
- while(q.size()>0){
- suma++;
- int x=q.front().first, y=q.front().second;
- odw[x][y]=1;
- q.pop();
- for (int i=0; i<4; i++){
- if (odw[x+a[i]][y+b[i]]==0 && ob[x+a[i]][y+b[i]]!=inf){
- q.push({x+a[i], y+b[i]});
- odw[x+a[i]][y+b[i]]=1;
- }
- }
- }
- return suma;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n, k, maks = 0;
- cin >> n >> k;
- for (int i=1; i<=n; i++){
- for (int j=1; j<=n; j++){
- cin >> tab[i][j];
- maks=max(maks, tab[i][j]);
- }
- }
- for (int i=1; i<=n; i++){
- tab[i][0]=inf;
- tab[0][i]=inf;
- tab[i][n+1]=inf;
- tab[n+1][i]=inf;
- odw[i][0]=1;
- odw[0][i]=1;
- odw[i][n+1]=1;
- odw[n+1][i]=1;
- }
- int pocz = 0, kon = maks+1;
- int licz = 0;
- while(pocz+1<kon)
- {
- int sr = (pocz+kon)/2;
- //cout << pocz << " " << kon << " " << sr << endl;
- licz++;
- for (int i=1; i<=n; i++){
- for (int j=1; j<=n; j++){
- if (tab[i][j]<=sr)
- ob[i][j]=tab[i][j];
- else
- ob[i][j]=inf;
- }
- }
- /*for (int i=1; i<=n; i++){
- for (int j=1; j<=n; j++)
- cout << ob[i][j] << " ";
- cout << endl;
- }*/
- bool czy = 0;
- for (int i=1; i<=n; i++){
- for (int j=1; j<=n; j++){
- if (odw[i][j]==0 && ob[i][j]!=inf && bfs(i,j)>=k)
- czy=1;
- }
- }
- memset(odw, 0, maxn*maxn);
- for (int i=1; i<=n; i++){
- odw[i][0]=1;
- odw[0][i]=1;
- odw[i][n+1]=1;
- odw[n+1][i]=1;
- }
- if (czy==0)
- pocz=sr;
- else
- kon=sr;
- }
- cout << kon;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement