Advertisement
Guest User

Las

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1e3+3;
  5. const int inf = 1e9+5;
  6. int tab[maxn][maxn], ob[maxn][maxn];
  7. bool odw[maxn][maxn];
  8. int a[4]={1,0,-1,0};
  9. int b[4]={0,1,0,-1};
  10.  
  11. queue<pair<int,int> > q;
  12. int bfs(int k, int l){
  13.     q.push({k,l});
  14.     int suma = 0;
  15.     while(q.size()>0){
  16.         suma++;
  17.         int x=q.front().first, y=q.front().second;
  18.         odw[x][y]=1;
  19.         q.pop();
  20.         for (int i=0; i<4; i++){
  21.             if (odw[x+a[i]][y+b[i]]==0 && ob[x+a[i]][y+b[i]]!=inf){
  22.                 q.push({x+a[i], y+b[i]});
  23.                 odw[x+a[i]][y+b[i]]=1;
  24.             }
  25.         }
  26.     }
  27.     return suma;
  28. }
  29.  
  30. int main() {
  31.     ios_base::sync_with_stdio(0);
  32.     cin.tie(0);
  33.     int n, k, maks = 0;
  34.     cin >> n >> k;
  35.     for (int i=1; i<=n; i++){
  36.         for (int j=1; j<=n; j++){
  37.             cin >> tab[i][j];
  38.             maks=max(maks, tab[i][j]);
  39.         }
  40.     }
  41.     for (int i=1; i<=n; i++){
  42.         tab[i][0]=inf;
  43.         tab[0][i]=inf;
  44.         tab[i][n+1]=inf;
  45.         tab[n+1][i]=inf;
  46.         odw[i][0]=1;
  47.         odw[0][i]=1;
  48.         odw[i][n+1]=1;
  49.         odw[n+1][i]=1;
  50.     }
  51.     int pocz = 0, kon = maks+1;
  52.     int licz = 0;
  53.     while(pocz+1<kon)
  54.     {
  55.         int sr = (pocz+kon)/2;
  56.         //cout << pocz << " " << kon << " " << sr << endl;
  57.         licz++;
  58.         for (int i=1; i<=n; i++){
  59.             for (int j=1; j<=n; j++){
  60.                 if (tab[i][j]<=sr)
  61.                     ob[i][j]=tab[i][j];
  62.                 else
  63.                     ob[i][j]=inf;
  64.             }
  65.         }
  66.         /*for (int i=1; i<=n; i++){
  67.             for (int j=1; j<=n; j++)
  68.                 cout << ob[i][j] << " ";
  69.             cout << endl;
  70.         }*/
  71.         bool czy = 0;
  72.         for (int i=1; i<=n; i++){
  73.             for (int j=1; j<=n; j++){
  74.                 if (odw[i][j]==0 && ob[i][j]!=inf && bfs(i,j)>=k)
  75.                     czy=1;
  76.             }
  77.         }
  78.         memset(odw, 0, maxn*maxn);
  79.         for (int i=1; i<=n; i++){
  80.             odw[i][0]=1;
  81.             odw[0][i]=1;
  82.             odw[i][n+1]=1;
  83.             odw[n+1][i]=1;
  84.         }
  85.         if (czy==0)
  86.             pocz=sr;
  87.         else
  88.             kon=sr;
  89.     }
  90.     cout << kon;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement