题目
题意翻译
题意简述:现在给一个N*N的矩阵,找一条路径从左上角走到右下角,每次可以向上下左右四个方向中某个方向走。要求走过的点中,数字最大的减去最小的。要求值越小越好。现在就是要求这个值。

输入格式: 第一行给出一个数字N(2 <= N <= 100),代表矩阵的大小。接下来一个N行N列的矩阵,里面每个数字的值在[0,110]之间。

输出格式: 一个数字,如翻译中所述

题目描述
English VietnameseFarmer 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.

输入输出格式
输入格式:
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.
输出格式:
An integer that is the minimal height difference on the optimal path.

输入输出样例
输入样例#1:
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
输出样例#1:
2

思路
这道题用二分!!!
题目描述都给的很明显了好吗!!
n最大到100,这些值的最大值才110!
110用来二分,绝对快啊。
所以就二分差值,每次判断就可以了,函数都写bool类型就行了。

代码

 #include<bits/stdc++.h>
using namespace std;
int n,l,r=,mid;
int mp[][];
bool vis[][];
struct node
{
int x,y;
};
int dir[][]={{,},{,-},{,},{-,}};
bool bfs(int le,int ri)
{
if(mp[][]<le||mp[][]>ri)return ;
queue<node> q;
q.push((node){,});
vis[][]=;
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==n&&now.y==n)return ;
for(int i=;i<;i++)
{
int tx=now.x+dir[i][];
int ty=now.y+dir[i][];
if(<=tx&&tx<=n&&<=ty&&ty<=n&&!vis[tx][ty])
{
vis[tx][ty]=;
if(mp[tx][ty]>=le&&mp[tx][ty]<=ri)
q.push((node){tx,ty});
}
}
}
return ;
}
bool check(int d)
{
for(int low=;low+d<=;low++)
{
memset(vis,,sizeof(vis));
if(bfs(low,low+d))return ;
}
return ;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>mp[i][j];
}
}
while(l<r)
{
mid=(l+r)/;
if(check(mid))r=mid;
else l=mid+;
}
cout<<r;
return ;
}

【题解】Mountain Walking-C++的更多相关文章

  1. [USACO2003][poj2110]Mountain Walking(二分答案+bfs)

    http://poj.org/problem?id=2110 题意:给你一个n*n矩形(n<=100),每个位置上都有一个数字代表此处山的高度,要从(1,1)走到 (n,n),要求一条路径使得这 ...

  2. POJ 2110 Mountain Walking 二分+bfs

    传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...

  3. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. 【题解】poj 3162 Walking Race 树形dp

    题目描述 Walking RaceTime Limit: 10000MS Memory Limit: 131072KTotal Submissions: 4941 Accepted: 1252Case ...

  7. Walking Ant(一道有意思的蚂蚁游戏,bfs)

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  8. Walking Ant(bfs)

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  9. [USACO 12JAN]Mountain Climbing

    Description Farmer John has discovered that his cows produce higher quality milk when they are subje ...

随机推荐

  1. go 通过赋值给 _ 来忽略序号和值

    go 语言中 只要想忽略的值都需要用 下划线 _ 来代替 package main import "fmt" func main() {     pow := make([]int ...

  2. PHP的 parse_ini_file 解析配置文件

    解析配置文件: parse_ini_file 类似解析php.ini文件样 配置文件内容如下: Example #1 sample.ini 的内容 ; This is a sample configu ...

  3. Django之ORM相关操作

    一般操作 常用的13个操作 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> get(** ...

  4. 【计算几何】Water Testing

    Water Testing 题目描述 You just bought a large piece of agricultural land, but you noticed that – accord ...

  5. sqlserver时间戳

    SELECT DATEADD(S,1576464113 + 8 * 3600,'1970-01-01 00:00:00') --时间戳转换成普通时间 SELECT DATEDIFF(S,'1970-0 ...

  6. 关于MySQL的驱动org.gjt.mm.mysql.Driver

    今天看了一个比较老视频使用org.gjt.mm.mysql.Driver来驱动连接,便试了一下看看怎么样,结果一直连不上数据库,后来看了tomcat的后台发现有报这个问题,于是把驱动改成com.mys ...

  7. (七)easyUI之Accordion折叠面板:普通的静态面板

    一.普通的静态面板 前台 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  8. Java ShellSort

    Java ShellSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  9. Go part 5 结构体,方法与接收器

    结构体 结构体定义 结构体的定义只是一种内存布局的描述(相当于是一个模板),只有当结构体实例化时,才会真正分配内存空间 结构体是一种复合的基本类型,通过关键字 type 定义为 自定义 类型后,使结构 ...

  10. [转载]三十分钟理解:线性插值,双线性插值Bilinear Interpolation算法

    [转载]三十分钟理解:线性插值,双线性插值Bilinear Interpolation算法 来源:https://blog.csdn.net/xbinworld/article/details/656 ...