Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- using namespace std;
- struct node{
- int i,j;
- int w;
- bool operator < (const node& rhs)const
- {
- return w > rhs.w;
- }
- };
- bool inB(int I,int n){
- return (I >= 0 && I < n);
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- int sI,sJ;
- int arr[n][n];
- for(int i = 0 ; i < n ; i++){
- for(int j = 0 ; j < n ; j++){
- scanf("%d",&arr[i][j]);
- if(arr[i][j] == 0){
- sI = i;
- sJ = j;
- }
- }
- }
- bool visited[n][n];for(int i = 0 ; i < n ; i++)for(int j = 0 ; j < n ; j ++)visited[i][j] = false;
- priority_queue<node> pq;
- pq.push({sI,sJ,0});
- int ans = -2e9;
- int moveI[4] = {-1,0,1,0};
- int moveJ[4] = {0,1,0,-1};
- while(!pq.empty()){
- int uI = pq.top().i;
- int uJ = pq.top().j;
- int uW = pq.top().w;
- pq.pop();
- if(uW == 0 && ans != -2e9)break;
- if(visited[uI][uJ])continue;
- visited[uI][uJ] = true;
- if(uW > ans)ans = uW;
- for(int i = 0 ; i < 4 ; i++){
- int vI = uI + moveI[i];
- int vJ = uJ + moveJ[i];
- if(inB(vI,n) && inB(vJ,n) && !visited[vI][vJ]){
- pq.push({vI,vJ,arr[vI][vJ]});
- }
- }
- }
- printf("%d",ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement