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 ...
随机推荐
- poj2486--Apple Tree(树状dp)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7789 Accepted: 2606 Descri ...
- POJ 2352 Stars(线段树)
题目地址:id=2352">POJ 2352 今天的周赛被虐了. . TAT..线段树太渣了..得好好补补了(尽管是从昨天才開始学的..不能算补...) 这题还是非常easy的..维护 ...
- 使用Microsoft excel 2007 进行数据分析---环境配置
使用Microsoft excel 2007 进行数据分析---环境配置 使用前须要安装SQL server 2008 data mining Add-ins for Microsoft excel ...
- modSecurity规则学习(一)——配置文件
环境:modSecurity3.0,nignx1.13.8 modSecurity配置文件 1.nginx.conf server { listen ; modsecurity on; //启动mod ...
- Android SecurityException
public boolean checkNetwork() { boolean result = false; try { Context context = this.getApplicationC ...
- lunix 命令积累
.修改文件的拥有者 chown 用户:用户 文件 .切换账号 su 账号 . 追踪路由信息 traceroute 主机名
- [C#防止反编译].NET 产品版权保护方案 (.NET源码加密保护)
[C#防止反编译].NET 产品版权保护方案 (.NET源码加密保护) 标签: .net加密产品c#dll工具 2011-03-24 21:06 27009人阅读 评论(13) 收藏 举报 分类: C ...
- 【习题 8-11 UVA - 1615】Highway
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个村庄都有一个范围[li..ri] 只要在这个范围内放点都可以"支配"这个村庄. 则问题就等价于线段上有n个区 ...
- 洛谷——T1725 探险
http://codevs.cn/problem/1725/ 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descri ...
- 【技能】Ext.Viewport 实现左三右一排列方式。
1.Extjs 布局非常是灵活.可是吐槽下CSS,太难重写,想自己重构一套都难哎... var viewport = new Ext.Viewport({ layout:'border', items ...