Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. constexpr int maxn = 110;
  5.  
  6.  
  7. int dx[] = {0,0,-1,1};
  8. int dy[] = {-1,1,0,0};
  9. int a[maxn][maxn];
  10. int bio[maxn][maxn];
  11. int n,gmax=-1,gmin=1000;
  12. int cmin, cmax;
  13.  
  14. bool check(int x, int y){
  15.     return x >= 0 && x < n && y >= 0 && y < n &&
  16.         a[x][y] >= cmin && a[x][y] <= cmax && !bio[x][y];
  17. }
  18.  
  19. bool solve(){
  20.     queue<pair<int,int>> q;
  21.     q.push(make_pair(0,0));
  22.  
  23.     while(!q.empty()){
  24.         int x = q.front().first;
  25.         int y = q.front().second;
  26.         if(x == n-1 && y == n-1)
  27.             return true;
  28.         q.pop();
  29.         bio[x][y] = true;
  30.         //printf("nalazim se u %d %d\n",x,y);
  31.  
  32.         for(int i = 0; i < 4; ++i){
  33.             int sx = x + dx[i];
  34.             int sy = y + dy[i];
  35.            // printf("idem u %d %d\n",sx,sy);
  36.             if(!check(sx,sy)) continue;
  37.             q.push(make_pair(sx,sy));
  38.         }
  39.     }
  40.  
  41.     return false;
  42. }
  43.  
  44.  
  45. int main(){
  46. ios_base::sync_with_stdio(false);
  47. cin.tie(NULL);
  48.  
  49. scanf("%d",&n);
  50. for(int i = 0; i < n; ++i){
  51.     for(int j = 0; j < n; ++j){
  52.         scanf("%d",&a[i][j]);
  53.         gmax = max(gmax,a[i][j]);
  54.         gmin = min(gmin,a[i][j]);
  55.     }
  56. }
  57.  
  58. for(int i = 0; i <= gmax - gmin; ++i){ //iteriranje po svim mogucim razlikama
  59.     for(int j = gmin; j <= gmax - i; ++j){ // isprobavanje postoji li staza za sve moguce intervale duljine i
  60.         cmin = j;
  61.         cmax = j+i;
  62.        // printf("cmin je %d cmax je %d\n",cmin,cmax);
  63.         if(a[0][0] < cmin || a[0][0] > cmax) continue;
  64.         memset(bio,0,sizeof(bio));
  65.         if(solve()){
  66.             cout << i;
  67.             return 0;
  68.         }
  69.     }
  70. }
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement