classSolution{vector<vector<int>> dir ={{1,0},{0,1},{-1,0},{0,-1}};int n;public:/*** @param arr: the map* @return: the smallest target that satisfies from the upper left corner (0, 0) to the lower right corner (n-1, n-1)*/intmapJump(vector<vector<int>>&arr){// Write your code here.if(arr.empty())return0;n = arr.size();int l =0, r =100000, mid, ans;bool way =false;while(l <= r){mid =(l + r)/2;way =false;vector<vector<bool>>vis(n, vector<bool>(n,false));vis[0][0]=true;ok(arr,0,0, vis, mid, way);//DFSif(way)//有滿足要求的路徑{ans = mid;r = mid-1;}elsel = mid+1;}return ans;}voidok(vector<vector<int>>&arr,int x,int y, vector<vector<bool>>& vis,int target,bool& way){if(way)return;if(x==n-1&& y==n-1){way =true;return;}int i, j, k;for(k =0; k <4;++k){i = x + dir[k][0];j = y + dir[k][1];if(i >=0&& i < n && j >=0&& j < n &&!vis[i][j]&&abs(arr[i][j]-arr[x][y])<=target){vis[i][j]=true;ok(arr,i,j,vis,target,way);}}}};