POJ - 2110 Mountain Walking(BFS + 二分 + 枚举区间)

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

大致题意:从左上角走到右下角 地图上的值代表山峰高度 所有路都能走 但是要你找出所有路中高度差最小的路

解题思路:用二分去找高度差  再去枚举区间以内山峰的高度,如果用BFS能够走出来证明这高度差是可行的 再通过二分缩小高度差


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int MAXN = 105;
int MAP[MAXN][MAXN];
int vis[MAXN][MAXN];
int n;
using namespace std;

struct node
{
    int x, y;
    node (int _x, int _y) : x(_x), y(_y) {}
};

int bfs(int low, int high, int x = 1, int y = 1)//判断高度差以内的山峰高度能否走出去
{
    if (MAP[x][y] < low || MAP[x][y] > high)//一定要判断第一个山峰在不在高度区间内
        return -1;
    memset(vis, 0, sizeof(vis));
    queue<node> Q;
    Q.push(node(x, y));
    vis[x][y] = 1;
    while (!Q.empty())
    {
        node q = Q.front(); Q.pop();
        if (q.x == n && q.y == n)
            return 1;
        for (int i = 0; i < 4; i++)
        {
            int xx = q.x + dx[i], yy = q.y + dy[i];
            if (MAP[xx][yy] >= low && MAP[xx][yy] <= high
                && !vis[xx][yy] && xx >= 1 && xx <= n && yy >= 1 && yy <= n)//只走在高度区间以内的山峰
            Q.push(node(xx, yy));
            vis[xx][yy] = 1;
        }
    }
    return -1;
}

int check(int d)//枚举高度差以内的的山峰高度
{
    for (int low = 0; low + d <= 110; low++)
    {
        if (bfs(low, low + d) == 1)
            return 1;
    }
    return -1;
}

int BinarySearch(int L, int R)//用二分去找高度差
{
    while (L <= R)
    {
        int mid = (L + R) >> 1;
        int flag = check(mid);
        if (flag == 1)//如果能找到那么高度差小于等于mid的值
            R = mid - 1;
        else
            L = mid + 1;
    }
    return L;
}

int main()
{
    while (cin >> n)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
                cin >> MAP[i][j];
        }
        cout << BinarySearch(0, 110) << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值