【题解】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 ...
随机推荐
- Python之并行编程笔记
概述: 非并发: 1 程序由单个步骤序列构成 2 包含独立子任务的程序执行性能低 并发: 1 异步.高效 2 分解子任务.简化流程与逻辑 进程process:1 一个程序的执行实例 2 每个进 ...
- 5-9 c语言之【初识win32编程】
---恢复内容开始--- 今天学习了win32的相关知识,首先win32是指是指可以在32位或以上Windows系统中运行的程序,我学习的主要利用c/c++语言编写的win32程序, 首先在win32 ...
- Django中的Object Relational Mapping(ORM)
ORM 介绍 ORM 概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用 ...
- 列表初始化(list initialization)
列表初始化啊就是大括号来初始化: 列表初始化的好处:
- 网络知识(1)TCP/IP五层结构
图1 数据流向图 1,网络基础 1.1 发展 古代:①烽火狼烟最为原始的0-1单bit信息传递:②飞鸽传书.驰道快马通信,多字节通信: 近代:①轮船信号灯:②无线电报[摩尔斯码]: 现代:①有线模拟通 ...
- JDBC 复习2 存取mysql 大数据
大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据 mysql的大数据分为2种 blob 和 text ,没有cl ...
- ConfigParser读取配置文件时报错:ConfigParser.MissingSectionHeaderError
使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...
- 关于vue.js的部分总结
1.MVVM和MVC的区别: MVVM:是Model-View-ViewModel的简写,即模型-视图-视图模型 模型:后端传递的数据 试图:所看到的页面 视图模型:mvvm模式的核心,它是连接vie ...
- Windows10+Android Studio 3.5编译项目报错——NDK Resolution Outcome: Project settings: Gradle model version=4.10.1, NDK version is UNKNOWN
项目背景: 系统有C.D两个盘,Android Studio安装在D盘,sdk安装在C盘. 出现的问题: 从git拉取项目后,一直编译不通过,提示“NDK Resolution Outcome: Pr ...
- Spring Boot笔记
@SpringBootApplication中有以下注解:@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Document ...