Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using pii = pair<int,pair<int,int> >;
- int n;
- int land[1005][1005];
- priority_queue<pii,vector<pii>,greater<pii> > Q;
- int dx[4] = {0,0,1,-1};
- int dy[4] = {1,-1,0,0};
- bool visited[1005][1005];
- bool checkpos(int x,int y){
- return x>=1 and x<=n and y>=1 and y<=n;
- }
- int main(){
- scanf("%d",&n);
- int x,y;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=n;j++){
- scanf("%d",&land[i][j]);
- if(land[i][j] == 0 and !start){
- x = i;
- y = j;
- break;
- }
- }
- }
- Q.push({-1,{x,y}});
- int weight_max = -1;
- while(!Q.empty()){
- x = Q.top().second.first;
- y = Q.top().second.second;
- int w = Q.top().first;
- Q.pop();
- if(w==0){
- printf("%d",weight_max);
- break;
- }
- visited[x][y] = true;
- weight_max = max(w,weight_max);
- for(int i=0;i<4;i++){
- int k = x+dx[i];
- int j = y+dy[i];
- if(checkpos(k,j) and !visited[k][j]){
- Q.push({land[k][j],{k,j}});
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement