catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索
题意
john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个单位,3. 移动到当前位置的二倍处。输出移动的最少次数。
解题思路
使用搜索,准确地说是广搜,要记得到达的位置要进行标记,还有就是减枝。
详情见代码实现。
代码实现
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7; //这个是数轴的最大尺度
int n, k, ans=inf;
struct node{
	int x, t;
};
map<int, int> mp; //使用map进行标记
queue<node> q;
void bfs(int x)
{
	mp.clear();
	int tx;
	node h={x, 0};
	q.push(h);
	mp[x]=1; //起点也要进行标记
	while(!q.empty() )
	{
		h=q.front();
		q.pop();
		tx=h.x+1;
		if(mp[tx]==0)
		{
			if(tx == k)
			{
				ans=min(ans, h.t+1);
				return ;
			}
            //只有当john的位置小于牛的位置时,进行加一操作才有意义
			else if(tx < k && tx<maxn)
			{
				node tmp={tx, h.t+1};
				q.push(tmp);
				mp[tx]=1;
			}
		}
		tx=h.x-1;
		if(mp[tx]==0)
		{
			if(tx == k)
			{
				ans=min(ans, h.t+1);
				return ;
			}
			else if(tx >= 0)//减一操作后要判断是不是小于0
			{
				node tmp={tx, h.t+1};
				q.push(tmp);
				mp[tx]=1;
			}
		}
		tx=h.x<<1;
		if(mp[tx]==0)
		{
			if(tx == k)
			{
				ans=min(ans, h.t+1);
				return ;
			}
			else if( h.x < k && tx<maxn) //只有起点小于k时,乘2操作开可以进行
			{
				node tmp={tx, h.t+1};
				q.push(tmp);
				mp[tx]=1;
			}
		}
	}
}
int main()
{
	scanf("%d%d", &n, &k);
	if(n==k)
	ans=0;
	else bfs(n);
	printf("%d\n", ans);
	return 0;
}
												
											catch that cow POJ 3278 搜索的更多相关文章
- Catch That Cow POJ - 3278  [kuangbin带你飞]专题一 简单搜索
		
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
 - kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278
		
题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...
 - (广搜)Catch That Cow -- poj -- 3278
		
链接: http://poj.org/problem?id=3278 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6211 ...
 - Catch That Cow POJ - 3278  bfs map超时,短路判断顺序。
		
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
 - C - Catch That Cow POJ - 3278
		
//标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...
 - poj 3278 搜索
		
描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediate ...
 - HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
		
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
 - poj 3278 Catch That Cow (bfs搜索)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
 - POJ 3278 Catch That Cow(BFS,板子题)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
 
随机推荐
- vuex中this.$store.dispatch和this.$store.commit的区别(都是调用vuex中的方法。一个异步一个同步)
			
dispatch:含有异步操作,例如向后台提交数据,写法: this.$store.dispatch('action方法名',值) commit:同步操作,写法:this.$store.commit( ...
 - C#调用Python(二)
			
python文件中有引入其他包.模块 一.源码 1.1 python源码,源码.python 打包方法,以及打包后的程序文件.请移步https://www.cnblogs.com/zhuanjiao ...
 - 微信小程序访问后台出现 对应的服务器证书无效。控制台输入 showRequestInfo() 可以获取更详细信息。
			
检查微信开发者平台配置 https 服务端 nginx 配置 ssl 协议是否有效 在开发者工具中可以使用(详情 > 不校验合法域名.web-view(业务域名).TLS 版本以及 HTTPS ...
 - SVG相关学习(一)SVG基础
			
SVG 相关学习 SVG SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG viewBox <svg width="500" heigh ...
 - 将数据库中带出的列,在gridview中影藏起来
			
前台增加事件:OnRowCreated="GridView1_RowCreated" protected void GridView1_RowCreated(object send ...
 - webpack插件之htmlWebpackPlugin
			
webpack插件之htmlWebpackPlugin webpack插件 自动化 htmlWebpackPlugin 由于webpack已经帮我们处理好js之间的依赖关系,现在我们可以忽略js的加 ...
 - rosbag record and play
			
话题录制: 录制所有发布出来的话题,此时默认将话题保存在一个以当时时间戳命名的文件夹中: $ rosbag record -a1 录制指定话题: $ rosbag record /topic1 ...
 - Linux驱动开发8——中断处理
			
中断包括软中断和硬中断两种,中断是一种异步I/O机制,即中断可以发生在任意时间点. 1.硬中断 硬件中断包括触发中断和处理中断两部分,而维系两者的是中断号,中断号是一种硬件资源. 1.1.注册和释放中 ...
 - python读取文件时遇到非法字符的处理  UnicodeDecodeError: 'gbk' codec can't decode bytes in position
			
报错UnicodeDecodeError: 'gbk' codec can't decode bytes in position ipath = 'D:/学习/语料库/SogouC.mini/Samp ...
 - js监听当前页面再次加载
			
document.addEventListener("visibilitychange", function () { if (!document.hidden) { //处于当前 ...