SuitNdtie

2Bk PROG1149

Mar 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4.  
  5. int arr[1010][1010];
  6. int n;
  7. int startI[2];
  8. int startJ[2];
  9. int ans = 1e9;
  10. struct edge{
  11.     int charm;
  12.     int maxc;
  13.     int y;
  14.     int x;
  15.     bool operator<(const edge& rhs)const
  16.     {
  17.         return charm > rhs.charm;
  18.     }
  19. };
  20.  
  21. void findmin(){
  22.     int move[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};
  23.     priority_queue<edge> pq;
  24.     pq.push({0,0,startI[0],startJ[0]});
  25.     bool visited[n][n];
  26.     for(int i=0;i<n;i++){
  27.         for(int j=0;j<n;j++){
  28.             visited[i][j] = false;
  29.         }
  30.     }
  31.     while(!pq.empty()){
  32.         int charm = pq.top().charm;
  33.         int lmax = pq.top().maxc;
  34.         int nowY = pq.top().y;
  35.         int nowX = pq.top().x;
  36.         pq.pop();
  37.         if(!visited[nowY][nowX]){
  38.         //  printf("Test y : %d , x : %d\n",nowY,nowX);
  39.             if(nowY == startI[1] && nowX == startJ[1]){
  40.         //      printf("Test %d\n",lmax);
  41.                 ans = min(ans,lmax);
  42.             }else{
  43.                 visited[nowY][nowX] = true;
  44.                 for(int i=0;i<4;i++){
  45.                     int nY = nowY+move[i][0];
  46.                     int nX = nowX+move[i][1];
  47.                     if(0 <= nY && nY < n  &&  0 <= nX && nX < n){
  48.                         if(!visited[nY][nX]){
  49.                             int nextmax;
  50.                             if(arr[nowY][nowX] != 0){
  51.                                 nextmax = max(arr[nY][nX],lmax);
  52.                             }else{
  53.                                 nextmax = arr[nY][nX];
  54.                             }
  55.                         //  printf("Test %d\n",nextmax);
  56.                             pq.push({arr[nY][nX] , nextmax , nY , nX});
  57.                         }  
  58.                     }
  59.                 }
  60.             }
  61.            
  62.         }
  63.     }
  64. }
  65.  
  66. int main()
  67. {
  68.     scanf("%d",&n);
  69.     int stc = 0;
  70. //  priority_queue<int,vector<int> ,greater<int> > q;
  71.     for(int i=0;i<n;i++){
  72.         for(int j=0;j<n;j++){
  73.             scanf("%d",&arr[i][j]);
  74.             if(arr[i][j] == 0){
  75.                 startI[stc] = i;
  76.                 startJ[stc] = j;
  77.                 stc++;
  78.             }else{
  79.     //          q.push(arr[i][j]);
  80.             }
  81.         }
  82.     }
  83. //  printf("start y : %d , x : %d\nstop y : %d , x : %d\n",startI[0],startJ[0],startI[1],startJ[1]);
  84.     findmin();
  85.     printf("%d",ans);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment