【题解】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 ...
随机推荐
- Anaconda安装报错
通用解决方案:先卸载,然后重新安装(注意安装路径全英文且不要有空格),勾选添加环境变量选项
- 1187: 零起点学算法94——今年暑假不AC(Java)
1187:零起点学算法94--今年暑假不AC Time Limit: 1 Sec Memory Limit: 32 MB 64bit IO Format: %lld Description " ...
- diy操作系统 附录:gcc栈帧开启与关闭
在gcc命令行参数中可以使用-fno-omit-frame-pointer来开启栈帧的使用,或者使用-fomit-frame-pointer选项来关闭. 然而,也可以针对某一个函数进行配置方法如下,这 ...
- (七)Redis之Keys的通用操作
package myRedis01; import java.util.HashMap; import java.util.List; import java.util.Map; import jav ...
- C#工厂模式案例
class JianDanGongChang { static void Main(string[] args) { Factory factory=new LianXiangFactory(); D ...
- C#基础--go to
goto语句的用法非常灵活,你可以用它实现很多功能,但是由于goto语句的跳转影响程序的结构,在使用的时候会使人迷茫,所以一般"教材"上都不建议使用,但是用它可以实现递归,循环,选 ...
- 还在用ABAP进行SAP产品的二次开发?来了解下这种全新的二次开发理念吧
Jerry从2018年底至今,已经写了一系列关于SAP Kyma的文章,您可以移步到本文末尾获得这些文章的列表.Kyma是SAP开源的一个基于Kubernetes的云原生应用开发平台,能够允许SAP的 ...
- Java学习第四天之标识符与关键字
Java语言和其他编程语言一样,使用标识符作为变量.对象的名字,也提供了系列的关键字用以实现特别的功能. 一.分隔符 Java语言里的分号(;).花括号({}).方括号([]).圆括号(()).空格. ...
- python获取字符串的前几个字符(包含汉字)
一个简单的字符串,比如a="小明xiaoming"或者b="小xiao明ming".想在只想得到字符串的前4个元素,a1="小明xi",b= ...
- PropTypes没有定义的问题
今天做项目遇到了一个坑 import React, { Component,PropTypes} from 'react'; console.log(PropTypes); //undefined 用 ...