题目
题意翻译
题意简述:现在给一个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. LaTeX 一些用法实例(并列图片、并列表格、算法代码示例、页眉太长、下划线,等)

    横向并列两个图片 \begin{figure} \begin{minipage}{0.49\linewidth} \centering \includegraphics[width=6.5cm]{Si ...

  2. 数值类型与std::string的相互转换

    1.使用std::stringstream: //将in_value值转换成out_type类型 template<class out_type, class in_value> out_ ...

  3. python中检测某个变量是否有定义

    目录 第一种方法使用内置函数locals() 第二种方法使用内置函数dir() 第三种方法使用内置函数vars() 第一种方法使用内置函数locals() 'testvar' in locals(). ...

  4. Scratch运动模块——有趣的弹球游戏(一)

    大家好!我是蓝老师,有了前几期Scratch的基础,相信大家早已摩拳擦掌,跃跃欲试了,甚至还有些小伙伴已经编写了非常不错的程序. 学习编程就是这样不断探索.主动思考.解决问题的过程. 本期内容: 课程 ...

  5. electron窗口相关操作(放大缩小退出,可拖动,可resize等)

    如下是对窗口最大化,最小化等相关操作: import { ipcMain, ipcRenderer, remote } from 'electron' import is from 'electron ...

  6. Asp.Net Core 2.0 之旅---@Html.Action

    原文:Asp.Net Core 2.0 之旅---@Html.Action 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  7. Form key length limit 2048 exceeded ,提交数据时,数据的键过长 或者是上传文件过大

    在ASP.NET Core MVC中,文件的key 默认最大为2048,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如 ...

  8. ubuntu装openssh-client和openssh-server

    1. 修改update源 进入/etc/apt/目录,首先用cp命令将sources.list备份成sources.list.bk,然后复制http://www.cnblogs.com/eastson ...

  9. 南宁AI项目

    1.能了解并对项目整体进度情况有清晰的认识,什么时间点需要完成什么工作项. 2.认识了解项目干系人,能和客户独立沟通交流,理解现场业务,不要一问三不知,什么情况都不了. 3.能推动项目进展和问题及时处 ...

  10. GraphQL实战篇(一)

    看过基础篇的都知道,GraphQL创建Schema有两种方式,Schema First和Graph Type,前者使用GraphQL Schema Language类似于EF的DB First:后者和 ...