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 ...
随机推荐
- Cocos2d-x游戏的一般验证分析
Coco2d-x引擎是相对于Unity3D的又一实力派引擎.尽管随着3D游戏的热门,很多其它的厂商偏向于Unity3D.可是Coco2d-x的普及量也不容小觑,特别是一些比較大的手游公司.比方触控科技 ...
- java中TCP传输协议
class TcpClient { public static void main(String[] args) throws Exception { //创建client的socket服务,指定目的 ...
- UML类图中的几种关系总结
UML类图,描写叙述对象和类之间相互关系的方式包含:依赖(Dependency).关联(Association).聚合(Aggregation).组合(Composition).泛化(G ...
- 闲的无聊写了个很(wu)有(liao)意(dao)思(bao)的程序
下午机房断网了 闲的无聊,写了个小游戏 忘了sleep在哪个库里了.. 自带变色效果哦 #include<iostream> #include<cstdio> #include ...
- lunix 命令积累
.修改文件的拥有者 chown 用户:用户 文件 .切换账号 su 账号 . 追踪路由信息 traceroute 主机名
- 【CS Round #39 (Div. 2 only) C】Reconstruct Sum
[Link]:https://csacademy.com/contest/round-39/task/reconstruct-sum/ [Description] 给你一个数字S; 让你找有多少对A, ...
- hdu5400Arithmetic Sequence
//一个序列,两个公差d1,d2 //问有多少个区间使得这个区间存在一个点,它的左边是公差为d1的序列 //它的右边是公差为d2的序列 //直接存入每一个点向左和向右延伸的公差长度,乘一下即可 //还 ...
- 【Java编码准则】の #01限制内存中敏感数据的生命周期
当竞争对手的应用程序与我们的应用程序执行在同一个系统上时,我们的应用程序在内存中的敏感数据是非常easy被竞争对手获取的.假设我们的应用程序符合以下几种情况之中的一个,那么竞争对手能够获取到我们应用的 ...
- Leetcode47: Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 推断一个链表是不是回文的,一个比較简单的办法是把链表每一个结点的值存在vect ...
- popover弹出框
<style> #view{width: 300px;height: 200px;border: 1px solid red;} </style> 以上是为了viewport更 ...