Poj 2110 Mountain Walking enumeration + bfs
Question:
To give an n * n matrix, we need to go from the upper left corner to the lower right corner to minimize the difference between the maximum number and the minimum number.
Analysis:
At first, I thought of the binary difference, and then determined whether there was a path. If I only knew the difference at a time, the maximum and minimum values along the way would be recorded in the deep search, if the node records only the x and y coordinates of a wide search, the search involves re-accessing previously accessed nodes, for example, starting from () to>) -> (). If the value of () is more appropriate, the optimal access path is ()-> (), that is) to be accessed again, the principle of only one access to each node is not met. It is obviously not good to add the node dimension to record the x and y coordinates of each node and reach the minimum maximum value it passes through. Later, we learned that we can increase the enumeration, not only the enumeration difference, but also the maximum value on the enumeration path. In this way, the maximum and minimum values on each path are determined and can be searched extensively.
//poj 2110//sep9#include
#include
using namespace std;const int maxN=128;struct Node{int x,y;};int g[maxN][maxN];int vis[maxN][maxN];int dirx[4]={0,0,-1,1};int diry[4]={-1,1,0,0};int n,diff,min_hight,max_hight;queue
q;bool pass(int low,int high){memset(vis,0,sizeof(vis));Node a;a.x=1,a.y=1;if(g[1][1]>high||g[1][1]
=1&&nx<=n&&ny>=1&&ny<=n&&vis[nx][ny]==0&&g[nx][ny]<=high&&g[nx][ny]>=low){Node a;a.x=nx,a.y=ny;q.push(a);vis[nx][ny]=1;if(nx==n&&ny==n) return true;}}}return false;}bool work(int mid){for(int high=min_hight;high<=max_hight;++high){int low=high-mid;if(pass(low,high))return true;}return false;}int main(){scanf("%d",&n);min_hight=INT_MAX;max_hight=INT_MIN; for(int i=1;i<=n;++i)for(int j=1;j<=n;++j){scanf("%d",&g[i][j]);min_hight=min(min_hight,g[i][j]);max_hight=max(max_hight,g[i][j]); }int ans,l=0,r=max_hight+1,mid;while(l