【题解】Mountain Walking-C++
题目
题意翻译
题意简述:现在给一个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++的更多相关文章
- [USACO2003][poj2110]Mountain Walking(二分答案+bfs)
http://poj.org/problem?id=2110 题意:给你一个n*n矩形(n<=100),每个位置上都有一个数字代表此处山的高度,要从(1,1)走到 (n,n),要求一条路径使得这 ...
- POJ 2110 Mountain Walking 二分+bfs
传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...
- OJ题解记录计划
容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001 A+B Problem First AC: 2 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- 【题解】poj 3162 Walking Race 树形dp
题目描述 Walking RaceTime Limit: 10000MS Memory Limit: 131072KTotal Submissions: 4941 Accepted: 1252Case ...
- Walking Ant(一道有意思的蚂蚁游戏,bfs)
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- Walking Ant(bfs)
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- [USACO 12JAN]Mountain Climbing
Description Farmer John has discovered that his cows produce higher quality milk when they are subje ...
随机推荐
- 矩阵拿宝物--Codeforces 1201D - Treasure Hunting Codeforces Round #577 (Div. 2)
网上题解比较少,自己比较弱研究了半天(已经过了),希望对找题解的人有帮助 题目链接:https://codeforc.es/contest/1201/problem/D 题意: 给你一个矩形,起始点在 ...
- 【Python | opencv+PIL】常见操作(创建、添加帧、绘图、读取等)的效率对比及其优化
一.背景 本人准备用python做图像和视频编辑的操作,却发现opencv和PIL的效率并不是很理想,并且同样的需求有多种不同的写法并有着不同的效率.见全网并无较完整的效率对比文档,遂决定自己丰衣足食 ...
- GoF 的 23 种设计模式的分类和功能
1. 根据目的来分 根据模式是用来完成什么工作来划分,这种方式可分为创建型模式.结构型模式和行为型模式 3 种. 创建型模式:用于描述“怎样创建对象”,它的主要特点是“将对象的创建与使用分离”.GoF ...
- js 颜色随机切换
生成随机颜色 方法1:RGB模式 function randomColor1() { var r=Math.floor(Math.random()*256); var g=Math.floor(Mat ...
- Python——类和对象(一)
一.定义类 在面向对象的程序设计中有两种重要概念: 类:可以理解为一个种类,一个模型,是一种抽象的东西. 实例.对象:可以理解为一种具体制作或者存在的东西. 定义类的语法格式如下: class 类名: ...
- prometheus+grafana监控redis
prometheus+grafana监控redis redis安装配置 https://www.cnblogs.com/autohome7390/p/6433956.html redis_export ...
- Luogu4022 CTSC2012熟悉的文章(广义后缀自动机+二分答案+动态规划+单调队列)
对作文库中的串建出广义SAM,然后显然可以二分答案,二分之后考虑暴力dp,设f[i]为前i位最长匹配长度,显然有f[i]=max(f[i-1],f[j]+i-j) (i-j>=l&&am ...
- C#类型转换工具类
using System; namespace Com.AppCode.Extend { public static partial class Ext { #region 数值转换 /// < ...
- ES6语法 学习
ECMAScript 6,也被称为ECMAScript 2015是ECMAScript标准的最新版本.6是语言的一个重要更新,并第一次更新语言由于ES5 2009标准.现在主要JavaScript引擎 ...
- ubantu18.04 配置nginx与uwsgi(前后端分离)
ubantu18.04 配置nginx与uwsgi 一.首先先安装nginx静态服务 先更新 sudo apt-get update 1.安装gcc g++的依赖库 sudo apt-get in ...