【二分】Honeymoon Hike

Honeymoon Hike
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2751 Accepted: 843

Description

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) onn 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


很容易看出来是二分。但是朴素的二分会超时,即二分答案。

如果换一个思路,二分了答案之后,枚举所有可能的上下界(长度固定的滑动区间),如果路径上的点不在这个区间里,就放弃。

至于为什么要快些,我主观上感觉是用多项式时间解决了非多项式复杂度问题的一部分(因为实际上如果是朴素的,确定了区间大小之后,他会在dfs的过程中确定上下界,这个显然用多项式时间处理好会快得多)。。这个思路感觉很巧妙呀!!!

有一点很重要(注释的地方),如果一条路径上不能到达终点,那么另一条与之重合后半部分的路径,必然不能达到终点,因此不能将标记取消,否则会超时。(以后这种跟最优的路径有关的题要注意这个地方!!!)


#include <cstdio>
#include <string>


#define MAX(a,b) ((a)>(b)?(a):(b)) 
#define MIN(a,b) ((a)<(b)?(a):(b)) 


long high;
long low;
const long inf = 0x7f7f7f7f;


long n;
long h[300][300];
bool used[300][300];
const long dx[] = {0,1,0,-1};
const long dy[] = {1,0,-1,0};


bool dfs(long x,long y)
{
	if (h[x][y] < low || h[x][y] > high)
		return false;
	if (x == n && y == n)
		return true;
			
	long nx,ny;
	
	for (long i=0;i<4;i++)
	{
		nx = x+dx[i];
		ny = y+dy[i];
		if (!used[nx][ny] && nx>0&&ny>0&&nx<n+1&&ny<n+1)
		{
			used[nx][ny] = true;
			if (dfs(nx,ny))
			{
//				used[nx][ny] = false;
				return true;
			}
//			used[nx][ny] = false;
		}
	}
	return false;
}


long getint()
{
	long rs=0;char tmp;bool sgn=1;
	do tmp = getchar();
	while (!isdigit(tmp)&&tmp-'-');
	if (tmp=='-'){tmp=getchar();sgn=0;}
	do rs=(rs<<3)+(rs<<1)+tmp-'0';
	while (isdigit(tmp=getchar()));
	return sgn?rs:-rs;
}


int main()
{
	freopen("hike.in","r",stdin);
	freopen("hike.out","w",stdout);
	
	long T = getint();
	for (long t=1;t<T+1;t++)
	{
		long maxh = -inf;
		long minh = inf;
		long ans = inf;
		n = getint();
//		for (long i=0;i<n+2;i++)
//			used[0][i]=used[n+1][i]=used[i][0]=used[i][n+1]=true;
		for (long i=1;i<n+1;i++)
			for (long j=1;j<n+1;j++)
			{
				h[i][j] = getint();
				maxh = MAX(maxh,h[i][j]);
				minh = MIN(minh,h[i][j]);
			}
		long l = 0;
		long r = maxh-minh;
		while (l <= r)
		{
			long mid = (l+r)>>1;
			long _low = h[1][1]-mid;
			bool flag = false;
			for (low=_low<0?0:_low;low<h[1][1]+1;low++)
			{
				high = low + mid;
				memset(used,0,sizeof used);
				used[1][1] = true;
				if (dfs(1,1))
				{
					flag = true;
					break;
				}
			}
			if (flag)
			{
				ans = MIN(ans,mid);
				r = mid - 1;
			}
			else
			{
				l = mid + 1;	
			}
		}
		printf("Scenario #%ld:\n%ld\n\n",t,ans);
	}
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值