Advertisement
DarkTXYZ

PROG-1149: บีคุนบันคุง (2Bk)

Apr 29th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using pii = pair<int,pair<int,int> >;
  5. int n;
  6. int land[1005][1005];
  7. priority_queue<pii,vector<pii>,greater<pii> > Q;
  8. int dx[4] = {0,0,1,-1};
  9. int dy[4] = {1,-1,0,0};
  10. bool visited[1005][1005];
  11.  
  12. bool checkpos(int x,int y){
  13.     return x>=1 and x<=n and y>=1 and y<=n;
  14. }
  15.  
  16. int main(){
  17.     scanf("%d",&n);
  18.     int x,y;
  19.     for(int i=1;i<=n;i++){
  20.         for(int j=1;j<=n;j++){
  21.             scanf("%d",&land[i][j]);
  22.             if(land[i][j] == 0 and !start){
  23.                 x = i;
  24.                 y = j;
  25.                 break;
  26.             }
  27.         }
  28.     }
  29.     Q.push({-1,{x,y}});
  30.     int weight_max = -1;
  31.     while(!Q.empty()){
  32.         x = Q.top().second.first;
  33.         y = Q.top().second.second;
  34.         int w = Q.top().first;
  35.         Q.pop();
  36.         if(w==0){
  37.             printf("%d",weight_max);
  38.             break;
  39.         }
  40.         visited[x][y] = true;
  41.         weight_max = max(w,weight_max);
  42.         for(int i=0;i<4;i++){
  43.             int k = x+dx[i];
  44.             int j = y+dy[i];
  45.             if(checkpos(k,j) and !visited[k][j]){
  46.                 Q.push({land[k][j],{k,j}});
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement