【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)

题干:

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. 

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute 
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute. 

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

解题报告

   这题用bfs去递推(其实也相当于dp啦,可以理解成  只是不是按照for循环遍历的顺序去dp而已)

AC代码:

#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
bool vis[300000 + 5];
int n,k;
struct Node {int pos,step;Node(){}Node(int pos,int step):pos(pos),step(step){}
};
int bfs(int x) {queue<Node> q;memset(vis,0,sizeof vis);q.push(Node(x,0));vis[x]=1;while(!q.empty()) {Node cur = q.front();q.pop();if(cur.pos == k) return cur.step;if( cur.pos+1<= 300000 && cur.pos+1 >= 0&&!vis[cur.pos+1] ){vis[cur.pos+1]=1;q.push(Node(cur.pos+1,cur.step+1));}if(cur.pos-1<= 300000&& cur.pos-1  >= 0 &&!vis[cur.pos-1]  ) {vis[cur.pos-1]=1;q.push(Node(cur.pos-1,cur.step+1));}if((cur.pos<<1) <= 300000 && (cur.pos<<1) >=0 &&!vis[cur.pos<<1]  ) {vis[cur.pos<<1]=1 ;q.push(Node(cur.pos<<1,cur.step+1)); 	}}return 0;
}
int main()
{while(~scanf("%d%d",&n,&k)) {printf("%d\n",bfs(n));}return 0;
}

错误代码:(dp)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
ll dp[3000000 +5];
inline ll min3(ll i,ll j,ll k) {if(i < j) {return i < k ? i : k;}if(j < k) return j;else return k;
}
int main()
{int n,k;while(~scanf("%d%d",&n,&k)) {memset(dp,INF,sizeof dp);for(int i = 1; i<=n; i++) {dp[i] = min(dp[i],(ll)n-(ll)i);dp[i*2] = min(dp[i*2],dp[i]+1);}if(k <= n) {printf("%lld\n",dp[k]);continue;}for(int i = n+1; i<=(k<<1); i++) {dp[i] = min3(dp[i],dp[i-1]+ 1,dp[i+1] + 1) ;dp[i*2] = min(dp[i*2],dp[i]+1);}printf("%lld\n",dp[k]);}return 0;
}

总结:

   这题有个很大的坑啊!!我刚开始不管数组开多大都是re,后来发现,,,数组不需要开多大,因为他有可能是负数!!所以需要判断是否越界。

   其二,开30W的数组就够了,不需要300W,但是有一点要注意,判断条件中,必须判断范围在前,判断vis数组在后。

      即,判断越界在前,判断其他在后。这一点非常重要!我立马想到了一般我写地图类dfs中的tx,ty也没大注意先判断越界还是先判断maze是否满足还是先判断vis是否标记过,这是因为!!!我的地图都是从1开始读入的,所以就算是先判断别的在判断txty是否越界,也不会出现re的情况,这一点是我今天才注意到的、、惭愧惭愧、、、if中判断顺序有的时候也是有区别的!!!有可能会导致越界!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441686.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 向公众号发送消息,微信公众号之主动给用户发送消息功能

前一段时间项目中遇到一个稍微麻烦一点的问题。即客户要求&#xff0c;他在后台编辑好文章后要主动给每个用户都发送消息&#xff0c;并可以让用户点击直接进入文章页面。于是乎&#xff0c;当时脑子一热&#xff0c;想着没什么大的问题&#xff0c;so easy。模板消息不就得了。…

【CodeForces - 764A】Taymyr is calling you (找规律,水题)

题干&#xff1a; Comrade Dujikov is busy choosing artists for Timofeys birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, …

ecshop php升级,升级-安装与升级- ECShop帮助

ECShop V2.6.2版本有GBK和UFT-8两种编码格式的程序&#xff0c;而低于ECShop V2.6.0 的版本只有UTF-8 编码的(这些版本包括 ECShop 2.5.1、ECShop 2.5.0、ECShop 2.1.5、ECShop 2.1.2b等)&#xff0c; 这些版本升级到ECShop V2.6.2均可以使用本程序升级。相同版本的升级只需要覆…

【CodeForces - 764B 】Timofey and cubes (模拟)

题干&#xff1a; Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Tim…

php页面转发,php如何实现页面路由转发

php实现页面路由转发的方法&#xff1a;首先配置nginx服务器&#xff0c;在【.htaccess】中写上nginx的语法&#xff1b;然后打开根目录的【index.php】&#xff0c;编写文件路由即可。php实现页面路由转发的方法&#xff1a;1、配置nginx服务器nginx服务器不会自动读取.htacce…

【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)

题干&#xff1a; One of Timofeys birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, bu…

php整站防注入程序,一个不错的php通用防注入程序

代码如下:function jk1986_checksql(){$bad_str "and|select|update||delete|insert|*";$bad_Array explode("|",$bad_str);/** 过滤Get参数 **/foreach ($bad_Array as $bad_a){foreach ($_GET as $g){if (substr_count(strtolower($g),$bad_a) > 0)…

【HDU - 5056】Boring count (尺取法)

题干&#xff1a; You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. Input In the first line there is an integer T , ind…

java图片上传被旋转,在其他大牛那看到的java手机图片上传旋转问题的解决方法...

// 将图片文件转化为字节数组字符串&#xff0c;并对其进行Base64编码处理//Base64所用包&#xff1a;org.apache.commons.codec.binary.Base64public static String encodeImgageToBase64(File imageFile) {byte[] data null;// 读取图片字节数组try {InputStream in new Fi…

【qduoj】奇数阶幻方 (构造)

题干&#xff1a; C语言_魔方阵 描述 魔方阵是一个古老的智力问题&#xff0c;它要求在一个mm的矩阵中填入1&#xff5e;m2的数字&#xff08;m为奇数&#xff09;&#xff0c;使得每一行、每一列、每条对角线的累加和都相等&#xff0c;如下为5阶魔方阵示例。 15 8 1 24 17…

【牛客161 - A】字符串(尺取法,桶标记法)

题干&#xff1a; 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 32768K&#xff0c;其他语言65536K 64bit IO Format: %lld 题目描述 小N现在有一个字符串S。他把这这个字符串的所有子串都挑了出来。一个S的子串T是合法的&#xff0c;当且仅…

mysql innodb 全表锁,Mysql InnoDB行锁及表锁分享

一. 背景知识二. 步入正题&#xff1a;表锁和行锁1.1. 表锁 vs 行锁在 MySQL 中锁的种类有很多&#xff0c;但是最基本的还是表锁和行锁&#xff1a;表锁指的是对一整张表加锁&#xff0c;一般是 DDL 处理时使用&#xff0c;也可以自己在 SQL 中指定&#xff1b;而行锁指的是锁…

【牛客 - 185A】无序组数 (思维,数学,因子个数)

题干&#xff1a; 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 131072K&#xff0c;其他语言262144K 64bit IO Format: %lld 题目描述 给出一个二元组&#xff08;A,B&#xff09; 求出无序二元组&#xff08;a,b&#xff09; 使得&#x…

php万能查询用预,PHP 与 mysql

一、php 的 sql 注入攻击1.1、什么是 sql 注入攻击用户提交一段数据库查询代码&#xff0c;根据返回的结果&#xff0c;获得某些他想得到的数据。比如 &#xff1a;查询某个管理员是否存在&#xff0c;一般程序员会这么写$sql "select * from user where nameluluyii and…

php 判断radio选中哪个,jquery如何判断单选按钮radio是否选中

jquery判断单选按钮radio是否选中的方法&#xff1a;1、加载页面的时候获取id&#xff0c;代码为【var fs$("#"id).val()】&#xff1b;2、点击按钮的时候获取id&#xff0c;代码为【var id $(this).attr("id")】。本教程操作环境&#xff1a;windows7系统…

【qduoj】【超级楼梯进阶版】

题干&#xff1a; 描述 N级阶梯&#xff0c;人可以一步走一级&#xff0c;也可以一步走两级&#xff0c;求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n&#xff0c;代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法&#xff0c;输出结…

matlab在光学实验中的应用,matlab在光学实验中的应用

matlab在光学实验中的应用 《MATLAB》课程论文MATLAB 在光学实验中的应用姓名&#xff1a;学号&#xff1a;专业&#xff1a;班级&#xff1a;指导老师&#xff1a;学院&#xff1a;完成日期&#xff1a;1MATLAB 在波动光学中的应用(姓名&#xff1a;郑苗苗 12012241736 2012 级…

【HDU - 6016】Count the Sheep (思维,类似二分图)

题干&#xff1a; Altough Skipping the class is happy, the new term still can drive luras anxious which is of course because of the tests! Luras became worried as she wanted to skip the class, as well as to attend the BestCoder and also to prepare for test…

如何生成时间序列matlab,求助:在MATLAB里如何输入时间序列中的时间

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼function[logRS,logERS,V]RSana(x,n,method,q)%Syntax:[logRS,logERS,V]RSana(x,n,method,q)%____________________________________________%% Performs R/Sanalysis on a time series.%% logRS is thelog(R/S).% logERS is theEx…

【CF#192 A】Funky Numbers (二分,查找,二元组)

题干&#xff1a; As you very well know, this years funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two…