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 ...
随机推荐
- 计算器-- 利用re模块 利用函数封装 第二版
import re remove_parentheses = re.compile('\([^()]+\)') def Remove_Parentheses(obj, s): # 找到内层的括号并且返 ...
- Exercise : Softmax Regression
Step 0: Initialize constants and parameters Step 1: Load data Step 2: Implement softmaxCost Implemen ...
- 使用Redis配置JAVA_环境
配置环境变量 1.安装完成后,右击"我的电脑",点击"属性",选择"高级系统设置": 2.选择"高级"选项卡,点击&qu ...
- 实现CSS样式垂直水平完全居中
1.水平居中 a.内联元素(inline or inline-*)居中? 你可以让他相对父级块级元素居中对齐 .center-children { text-align: center; } b.块级 ...
- module.exports,exports和export default,export的区别
前提:CommonJS模块规范和ES6模块规范是完全不同的两个概念. module.exports,exports属于CommonJS模块规范: export default,export属于ES6模 ...
- 2017国家集训队作业[arc076d/f][Exhausted?]
2017国家集训队作业[arc076d/f][Exhausted?] 题意: 有\(N\)个人,\(M\)把椅子,给出\(...L_i.R_i\)表示第\(i\)个人可以选择编号为\(1\sim ...
- SASS常用方法
cnpm install --save-dev sass-loader //sass-loader依赖于node-sass cnpm install --save-dev node-sass //实现 ...
- netstat---显示Linux中网络系统的状态信息
netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. 语法 netstat(选项) 选项 -a或--all:显示所有连线中的Socket: -A<网 ...
- Ueditor 七牛集成
UEDITOR修改成功的 http://blog.csdn.net/uikoo9/article/details/41844747 http://blog.csdn.net/u010717403/ar ...
- [51Nod]NOIP2018提高组省一冲奖班模测训练(二)
http://www.51nod.com/contest/problemList.html#!contestId=73&randomCode=4408520896354389006 还是原题大 ...