Honeymoon Hike POJ - 2922(dfs,二分)

Emma is on a hiking trip with Eric, her freshly-married husband, for their honeymoon. They are hiking from one cabin to the next every day. Unfortunately, Eric is not as fit as Emma and is slowly getting tired. Since Emma does not want to start their newly-formed marriage with a serious conflict (and needs somebody to keep her warm in the nights), she decides to plan the next day trips so that they are not so strenuous for Eric.

In the past days, Emma has discovered a surprising fact about her husband. He is not so much tired by the length of their daily trip or the total amount of meters they had to climb. Instead, Eric is tired the more, the bigger the difference between the highest and the lowest point of today’s route becomes. Emma assumes this is due to psychological factors. It just sounds a lot more difficult to climb once from 500 meter to 1,500 meters than to climb from 200 to 400 meters ten times, although you climbed twice as much in the latter case.

Given an altitude map of the terrain, you should help Emma in finding a path that minimizes the difference between its highest and its lowest elevation, so that Eric does not feel as tired. The cabin they start at is located at the top-left corner and their destination is the bottom-right corner of the map. They can move along any of the four major directions but not on a diagonal.

Input
The first line contains the number of scenarios. Each scenario starts with a number n (2 ≤ n ≤ 100), the size of the area. The elevations of the terrain are given as a n × n integer matrix (hi,j) (0 ≤ hi,j ≤ 200) on n lines, where each line contains n space-separated elevations.

Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then, print a single line containing the difference between the highest and the lowest elevation on the optimal path. Terminate the output for the scenario with a blank line.

Sample Input
1
5
1 1 3 6 8
1 2 2 5 5
4 4 0 3 3
8 0 2 2 4
4 3 0 3 1
Sample Output
Scenario #1:
3

题意: 求起点到终点的一条路径,使得路径上的最大值最小值差最小
思路: 最大值最小肯定是二分,二分这个差值。那么搜索什么呢?假设搜索记录过程中记录最大值最小值,判断不能超过差值,搜不了了就得回溯,这样复杂度很高。那么假设记录的是最大值和最小值,那么搜到了不可到的点,标记后,无论怎么走到不能再来这个点了,所以不需要回溯消除影响。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
const int maxn = 105;
int a[maxn][maxn],vis[maxn][maxn],n;
int dirx[] = {0,0,1,-1};
int diry[] = {1,-1,0,0};

bool dfs(int x,int y,int low,int high)
{
    vis[x][y] = 1;
    if(a[x][y] > high || a[x][y] < low)return false;
    if(x == n && y == n)return true;
    for(int i = 0;i < 4;i++)
    {
        int dx = x + dirx[i],dy = y + diry[i];
        if(dx >= 1 && dx <= n && dy >= 1 && dy <= n && !vis[dx][dy])
        {
            if(dfs(dx,dy,low,high))return true;
        }
    }
    return false;
}

bool check(int x)
{
    for(int i = 0;i + x <= 200;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(1,1,i,i + x))return true;
    }
    return false;
}

int main()
{
    int T;scanf("%d",&T);
    int kase = 0;
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        int l = 0,r = 200,ans = 200;
        while(r - l >= 0)
        {
            int mid = (l + r) >> 1;
            if(check(mid))
            {
                r = mid - 1;
                ans = mid;
            }
            else
            {
                l = mid + 1;
            }
        }
        printf("Scenario #%d:\n%d\n\n",++kase,ans);
    }
    
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值