Pku2110 Mountain Walking
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 38 Solved: 23
[Submit][Status][Web Board][Edit] [TestData]
Description
Farmer John and Bessie the cow have embarked on one of those 'active' vacations. They spend entire days walking in the mountains and then, at the end of the day, they tire and return to their vacation cabin.
Since climbing requires a lot of energy and they are already tired, they wish to return to the cabin using a path that has the least difference between its highest and lowest elevations, no matter how long that path is. Help FJ find this easy-to-traverse path.
The map of the mountains is given by an N x N (2 <= N <= 100) matrix of integer elevations (0 <= any elevation <= 110) FJ and Bessie are currently at the upper left position (row 1, column 1) and the cabin is at the lower right (row N, column N). They can travel right, left, toward the top, or toward the bottom of the grid. They can not travel on a diagonal.
给出一个地图,现在希望你从左上角走到右下角
求某一条路径,这条路径上的最高点与最低点差值越小越好.请输出这个值
Since climbing requires a lot of energy and they are already tired, they wish to return to the cabin using a path that has the least difference between its highest and lowest elevations, no matter how long that path is. Help FJ find this easy-to-traverse path.
The map of the mountains is given by an N x N (2 <= N <= 100) matrix of integer elevations (0 <= any elevation <= 110) FJ and Bessie are currently at the upper left position (row 1, column 1) and the cabin is at the lower right (row N, column N). They can travel right, left, toward the top, or toward the bottom of the grid. They can not travel on a diagonal.
给出一个地图,现在希望你从左上角走到右下角
求某一条路径,这条路径上的最高点与最低点差值越小越好.请输出这个值
Input
* Line 1: The single integer, N
* Lines 2..N+1: Each line contains N integers, each of which specifies a square's height. Line 2 contains the first (top) row of the grid; line 3 contains the second row, and so on. The first number on the line corresponds to the first (left) column of the grid, and so on.
* Lines 2..N+1: Each line contains N integers, each of which specifies a square's height. Line 2 contains the first (top) row of the grid; line 3 contains the second row, and so on. The first number on the line corresponds to the first (left) column of the grid, and so on.
Output
* Line 1: An integer that is the minimal height difference on the optimal path.
Sample Input
5
1 1 3 6 8
1 2 2 5 5
4 4 0 3 3
8 0 2 3 4
4 3 0 2 1
Sample Output
2
HINT
思路:二分+DFS
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 const int dx[4]={0,0,1,-1}; 6 const int dy[4]={1,-1,0,0}; 7 int a[201][201],n,used[201][201]; 8 9 10 bool dfs(int t,int w,int x,int y) 11 { 12 if(x==n&&y==n) return true; 13 for(int i=0;i<4;i++) 14 { 15 int tx=x+dx[i],ty=y+dy[i]; 16 if(tx<=0||ty<=0||tx>n||ty>n||used[tx][ty]) continue; 17 if(a[tx][ty]>=t&&a[tx][ty]<=w) 18 { 19 used[tx][ty]=1; 20 if(dfs(t,w,tx,ty)) 21 return true; 22 } 23 } 24 return false; 25 } 26 27 int main() 28 { 29 scanf("%d",&n); 30 for(int i=1;i<=n;i++) 31 for(int j=1;j<=n;j++) 32 scanf("%d",&a[i][j]); 33 int low=0,high=120,ans=120; 34 while(low<=high) 35 { 36 int mid=(low+high)>>1; 37 int mi=max(a[1][1]-mid,0),flag=0; 38 for(int i=mi;i<=a[1][1];i++) 39 { 40 memset(used,0,sizeof(used)); 41 used[1][1]=1; 42 if(dfs(i,i+mid,1,1)) 43 { 44 flag=1; 45 break; 46 } 47 } 48 if(flag) 49 { 50 ans=min(ans,mid); 51 high=mid-1; 52 } 53 else low=mid+1; 54 } 55 printf("%d ",ans); 56 return 0; 57 }
还有BFS写法(不是我写的)
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0}; 4 int mp[110][110],n,ans,minn=(1<<30)-1,maxx; 5 bool vis[110][110]; 6 struct fk{int x,y;}; 7 queue<fk> q; 8 int read() 9 { 10 int x=0,f=1; 11 char ch=getchar(); 12 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 13 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 14 return x*f; 15 } 16 bool bfs(int lst,int hst) 17 { 18 memset(vis,0,sizeof(vis)); 19 fk a;a.x=a.y=1; 20 if(mp[1][1]>hst||mp[1][1]<lst)return 0; 21 while(!q.empty())q.pop(); 22 q.push(a); 23 vis[1][1]=1; 24 while(!q.empty()) 25 { 26 fk t=q.front();q.pop(); 27 int x=t.x,y=t.y; 28 for(int i=0;i<4;i++) 29 { 30 int nx=x+dx[i],ny=y+dy[i]; 31 if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&!vis[nx][ny]&&mp[nx][ny]<=hst&&mp[nx][ny]>=lst) 32 { 33 a.x=nx,a.y=ny; 34 q.push(a); 35 vis[nx][ny]=1; 36 if(nx==n&&ny==n)return 1; 37 } 38 } 39 } 40 return 0; 41 } 42 int main() 43 { 44 //freopen("walking.in","r",stdin); 45 //freopen("walking.out","w",stdout); 46 n=read(); 47 for(int i=1;i<=n;i++) 48 for(int j=1;j<=n;j++) 49 mp[i][j]=read(),minn=min(minn,mp[i][j]),maxx=max(maxx,mp[i][j]); 50 int l,r=maxx; 51 while(l<=r) 52 { 53 int mid=(l+r)>>1,sum=0; 54 for(int i=minn;i<=maxx;i++) 55 { 56 int ll=i-mid; 57 if(bfs(ll,i))sum=1; 58 } 59 if(sum==1)r=mid-1,ans=mid; 60 else l=mid+1; 61 } 62 printf("%d",ans); 63 }