B - Mountain Walking

本文介绍了一个结合二分搜索与深度优先搜索解决特定图问题的算法案例。问题要求寻找从左顶点到右下角路径中权值差最小的路径。通过将最大权值与最小权值的差值作为二分的目标,实现了对图遍历的有效优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

大意给一个图,让你从左顶点走到右下角,且路径上最大的权值与最小的权值之差最小。

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

输出 2.

刚开始就想着暴搜能过几个样例。结果T的我怀疑人生,题目正解是个二分加暴搜,感觉时间复杂度也不低,但是过了。。。。

找出最大权值与最小权值,通过二分两者差值来找最优解,又理解了一下上次学习的二分算法,对图的遍历又有了一种新的体会

#include <iostream>
#include <cstring>
using namespace std;
int map[110][110],vis[110][110],n,mmax,cun[4][2]={{0,1},{0,-1},{1,0},{-1,0}},low,up;
int dfs(int x,int y)
{	vis[x][y]=1;
	if(x==n-1&&y==n-1)
	{
		return 1; 
	}
	for(int i=0;i<4;i++)
	{
		int xx=x+cun[i][0];
		int yy=y+cun[i][1];
		if(xx>=0&&xx<n&&map[xx][yy]>=low&&map[xx][yy]<=up&&yy>=0&&yy<n&&!vis[xx][yy])
		{	
			
			if(dfs(xx,yy))
			return 1;
		}
	}
	return 0;
}
bool judge(int tt)
{	int ss=map[0][0] 
	for(int i=0;i+tt<=111;i++)
	{	memset(vis,0,sizeof(vis));
			vis[0][0]=1;
			up=i+tt;
			low=i;
			if(dfs(0,0))
			return 1;	
	}	
    return 0;
} 
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			scanf("%d",&map[i][j]);
	int l=0,r=110;
	while(l<r)
	{
		int mod=(l+r)/2;
		  if(judge(mod)) r=mod;
		  else
		   l=mod+1;
	}
	cout<<l<<endl;
	return 0;
}

 

Description Mountain Watching [Jeffrey Wang, 2009] One day, Bessie was gazing off into the distance at the beautiful Wisconsin mountains when she wondered to herself: which mountain is the widest one? She decided to take N (1 <= N <= 100,000) equally-spaced height measurements H_i (1 <= H_i <= 1,000,000,000) sequentially along the horizon using her new Acme Long Distance Geoaltimeter. A mountain is defined to be a consecutive sequence of H_i values which increases (or stays the same) and then decreases (or stays the same), e.g., 2, 3, 3, 5, 4, 4, 1. It is possible for a mountain on the edge of her field of vision only to increase or only to decrease in height, as well. The width of a mountain is the number of measurements it encompasses. Help Bessie identify the widest mountain. Here's a simple example of a typical horizon: ******* * ********* *** ********** ***** *********** ********* * * ***************** *********** *** * ** ******************* ************* * * ******* * ********************************************************************** 3211112333677777776543332111112344456765432111212111112343232111111211 aaaaaa ccccccccccccccccccccc eeeeeee ggggggggg bbbbbbbbbbbbbbbbbbbbbbbbbbbb ddddd ffffffffff hhhhhhhhh The mountains are marked 'a', 'b', etc. Obviously, mountain b is widest with width 28. The mountain on the left has width 6 for the purposes of this task. Input * There are multiple test cases. * For each case: ** Line 1: A single integer: N ** Lines 2..N+1: Line i+1 contains a single integer: H_i Output * For each case: ** Line 1: A single line with a single integer that is the width of the widest mountain. Sample Input 7 3 2 3 5 4 1 6 INPUT DETAILS: The height measurements are 3, 2, 3, 5, 4, 1, 6. Sample Output 5 OUTPUT DETAILS: The widest mountain consists of the measurements 2, 3, 5, 4, 1. Other mountains include 3, 2 and 1, 6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值