POJ 2110 Mountain Walking(二分+BFS)
2016-05-17 22:29
330 查看
思路:二分高度差mid且我们枚举的是区间上下界up和low.(mid=up-low),对于一个区间[low,up]我们用BFS找,看看能不能找到一条从左上角到右下角的路,且路中走过的所有节点的高度值x都属于区间[low,up].
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.
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.
Output
* Line 1: An integer that is the minimal height difference on the optimal path.
Sample Input
Sample Output
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int maxn=105; int map[maxn][maxn]; int vis[maxn][maxn]; int n; int dr[]={-1,1,0,0};//上下左右 int dc[]={0,0,-1,1}; struct Node { int r,c; Node(int r,int c):r(r),c(c){} }; bool BFS(int low,int up) { if(map[1][1]<low || map[1][1]>up) return false; queue<Node> Q; Q.push(Node(1,1)); vis[1][1]=1; while(!Q.empty()) { Node node=Q.front();Q.pop(); int r=node.r,c=node.c; for(int dir=0;dir<4;dir++) { int nr=r+dr[dir] , nc=c+dc[dir]; if(nr>=1&&nr<=n&&nc>=1&&nc<=n&&!vis[nr][nc]) { vis[nr][nc]=1; if(map[nr][nc]>=low && map[nr][nc]<=up) { Q.push(Node(nr,nc)); if(nr==n&&nc==n) return true; } } } } return false; } bool check(int d) { for(int low=0;low+d<=110;low++) { memset(vis,0,sizeof(vis)); if(BFS(low,low+d)) return true; } return false; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&map[i][j]); int L=0,R=110; while(R>L) { int mid=(R+L)>>1; if(check(mid)) R=mid; else L=mid+1; } printf("%d\n",R); return 0; }
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.
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.
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
相关文章推荐
- UVA 10976 Fractions Again?! (暴力预处理)
- 什么是RAID? 硬RAID 和软RAID的区别是什么?
- Main函数中的argc和argv应用举例
- codeforces 675B Restoring Painting
- HDU 1022 Train Problem I (STL 栈模拟)
- Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题
- mysql-创建用户报错ERROR 1396 (HY000): Operation CREATE USER failed for 'XXXX'@'XXXX'
- 未来10年最可能出现颠覆性创新的9大领域,第一条就属于我们
- Unable to connect to a repository at URL '': authorization failed: Could not authenticate to server:
- fail-fast机制
- Could not find a storyboard named 'Main' in bundle NSBundle
- VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt
- main中的运行时(runtime)
- Understand images & containers
- Codeforces Round #353 (Div. 2) B. Restoring Painting(方块内放数,问有多少种不同的放法)
- svn cleanup failed–previous operation has not finished
- codeforces 675B B. Restoring Painting(暴力枚举)
- hmailserver和rounduce安装
- TNS-00510 IBM/AIX RISC System/6000 Error: 11: Resource temporarily unavailable
- Scaling your cloud——详解 Nova 中的 Region, Cell, Availability Zone, Host Aggregates Zone