POJ 3278 Catch That Cow(BFS 剪枝)
题目链接:http://poj.org/problem?id=3278
这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天。 = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来。0 0
先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置。
并且农夫有三种移动方式,X + 1,X - 1,X * 2。问最少几步抓到牛。
開始觉得非常easy的,三方向的BFS就能顺利解决。然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前。牛早跑了。
然后反应过来开标记。来节约时间,然后发现竟然RE了,预计是标记数组不够大,毕竟有一个方向的x * 2。
然后打算控制一下查找范围。由于当X到达某个范围之后,在变为K,肯定不会是最少路径。
比方。当X已经大于K了。再运行X + 1, X * 2,肯定不会最短的得到K,然后在推断标记的时候做了优化。
然后就过掉了。
我再去百度。看看有没有更好的方法,发现我这种方法就叫剪枝啊 = =。吓尿、
代码例如以下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> #define LEN 1000000
struct node
{
int num,t;
}q[LEN]; bool vis[10000000]; int bfs (int x,int k)
{
int s = 0,e = 0; q[s].num = x;
q[s++].t = 0;
vis[x] = 1;
while (s != e)
{
struct node tmp = q[e++];
e %= LEN; if (tmp.num == k)
return tmp.t; if (!vis[tmp.num + 1] && tmp.num <= k)
{
q[s].num = tmp.num + 1;
q[s++].t = tmp.t +1;
s %= LEN;
vis[tmp.num + 1] = 1;
} if (!vis[tmp.num - 1] && tmp.num - 1 >= 0)
{
q[s].num = tmp.num - 1;
q[s++].t = tmp.t +1;
s %= LEN;
vis[tmp.num - 1] = 1;
} if (!vis[tmp.num * 2] && tmp.num <= k)
{
q[s].num = tmp.num * 2;
q[s++].t = tmp.t +1;
s %= LEN;
vis[tmp.num * 2] = 1;
}
} return -1;
} int main()
{
int x,k; while (~scanf ("%d%d",&x,&k))
{
int ans = bfs (x,k); printf ("%d\n",ans);
}
return 0;
}
POJ 3278 Catch That Cow(BFS 剪枝)的更多相关文章
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
		
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
 - POJ 3278 Catch That Cow(BFS,板子题)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
 - 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: 61826 Accepted: 19329 ...
 - POJ - 3278 Catch That Cow BFS求线性双向最短路径
		
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
 - poj 3278 Catch That Cow bfs
		
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
 - poj   3278  Catch That Cow(bfs+队列)
		
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
 - POJ 3278 Catch That Cow bfs 难度:1
		
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
 - POJ - 3278   Catch That Cow    bfs    线性
		
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
 - BFS POJ 3278 Catch That Cow
		
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
 
随机推荐
- poj 1087 A Plug for UNIX(字符串编号建图)
			
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
 - HDU 2563 统计问题 (DFS + 打表)
			
统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
 - Android中关于Volley的使用(十)对Request和Reponse的认识
			
我们知道,在网络Http通信中.一定会有一个Request.相同的,也一定会有一个Response.而我们在Volley中利用RequestQueue来加入请求之前,一定会先创建一个Request对象 ...
 - Es61
			
ECMAScript和JavaScript的关系 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司将在这个标准的基础 ...
 - js--11对象的创建方式
			
<html> <head> <title>Object</title> </head> <body> <script ty ...
 - 7.Web Service 调用天气代码
			
1. 2500多个城市天气预报 WEB服务公用事业 Endpoint:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx Disco: ...
 - Android 实现QQ、微信、新浪微博和百度第三方登录
			
前言: 对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于第三方登录的实现,并拿出来与大家一同分享: 各大开放平台注册账户获取AppK ...
 - chsh---更换登录系统时使用的shell
			
chsh命令 chsh命令用来更换登录系统时使用的shell.若不指定任何参数与用户名称,则chsh会以应答的方式进行设置. 语法 chsh(选项)(参数) 选项 -s<shell 名称&g ...
 - 谈一谈Nginx的强大
			
什么是Nginx? Nginx是一款高性能,开源的,支持高并发而轻量级的Web服务器,同时也是具有反向代理服务器及电子邮件(IMAP/POP3)的代理服务器. 基于REST架构风格,并且以统一资源描述 ...
 - 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
			
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现. 进行一次操作过后. 得到的数字肯定是<=1000的 然后1000以下可以暴力做的. 则我们枚举第1步后得到的数字x是 ...