POJ 2110 Mountain Walking 暴力搜索+二分

题意:n*n的矩阵 每次能走四个方向,定义路径的花费为:路径中方格的max-min,问从左上到右下的最小花费,n<=100
4个方向不是DAG,不能DP,暴力搜索 每个点最坏情况下走n*n 共n*n个点 O(n^4)TLE
二分ans后 枚举下界,则此时知道路径的最小值和最大值从

起点出发把mn<=c<=mx的点都遍历,此时dfs 相当于遍历图,不用回溯,复杂度为O(n^3*logn)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const int N=3e2+20;

int n,ans,c[N][N],vis[N][N];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool dfs(int x,int y,int mn,int mx)
{ 

	if(c[x][y]>mx||c[x][y]<mn)
		return false;
	if(x==n&&y==n)
		return true;
	for(int i=0;i<4;i++)
	{
		int a=x+dx[i],b=y+dy[i];
		if(a>=1&&a<=n&&b>=1&&b<=n&&!vis[a][b])
		{
			vis[a][b]=1;
			if(dfs(a,b,mn,mx))
				return true;
		}
	}
	return false;
}
void solve()
{
	int l=0,r=110,ans;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		bool flag=false;
		for(int i=0;i<=c[1][1];i++)
		{
			memset(vis,0,sizeof(vis));
			vis[1][1]=1;
			if(dfs(1,1,i,i+mid))
			{
				flag=true;
				break;
			}
		}
		if(flag)
			ans=mid,r=mid-1;
		else
			l=mid+1;
	}
	cout<<ans<<endl;
}
int main()
{
	while(cin>>n)
	{
		ans=1e9;
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&c[i][j]);
			}
		}
		solve();
	}

	return 0;
}

  

时间: 2025-01-01 08:53:06

POJ 2110 Mountain Walking 暴力搜索+二分的相关文章

poj 2110 Mountain Walking 枚举+bfs

题意: 给一个n*n的矩阵,要从左上角走到右下角,使经过数字的最大数与最小数的差最小. 分析: 一开始想到了二分这个差,然后判断是否存在路径,每次只知道差的话深搜每次搜索要记录沿途的最大值和最小值会tle,广搜的话如果节点只记录x,y坐标,搜索中存在要重新访问以前访问过节点的情况,比如一开始(1,1)->(1,2)->(2,2),如果(2,1)这个点的值更合适,最优访问路径(1,1)->(2,1)->(2,2),也就是(2,2)要被重新访问,不满足广搜每个节点只访问一次的原则,只有

POJ 2110 Mountain Walking 二分+bfs

传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, lower从0开始枚举, 每一次bfs的时候, 如果一个点的值小于lower或者大于lower+x, 那么就不走这个点, 看最后能否走到终点. 自己的整数二分写的真是不忍直视...改了好久. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #defi

POJ 2785 (暴力搜索&amp;二分)

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks o

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

POJ 2329 (暴力+搜索bfs)

Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your pro

G - Shuffle&#39;m Up POJ 3087 模拟洗牌的过程,算作暴力搜索也不为过

G - Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3087 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by start

POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7488   Accepted: 2845 Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlo