HDU 2717 Catch That Cow --- BFS
题目大意:在x坐标上,农夫在n,牛在k。农夫每次可以移动到n-1, n+1, n*2的点。求最少到达k的步数。
思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先找到的一定是最小的步数。
- /* HDU 2717 Catch That Cow --- BFS */
- #include <cstdio>
- #include <cstring>
- #include <queue>
- using namespace std;
- bool visit[];
- int n, k;
- struct Node{
- int num;
- int step;
- Node(int lhs = , int rhs = ) :num(lhs), step(rhs){}
- };
- inline bool judge(int x){
- if (x < || x > || visit[x])
- return ;
- return ;
- }
- void bfs(){
- memset(visit, , sizeof visit);
- queue<Node> q;
- visit[n] = ; //标记起点已访问
- q.push(n);
- while (!q.empty()){
- Node x = q.front(); q.pop();
- if (x.num == k){
- printf("%d\n", x.step);
- break;
- }
- Node y = x;
- ++y.step;
- //第一个方向 x-1
- y.num = x.num - ;
- if (judge(y.num)){
- visit[y.num] = ; //标记该点已访问
- q.push(y);
- }
- //第二个方向 x+1
- y.num = x.num + ;
- if (judge(y.num)){
- visit[y.num] = ; //标记该点已访问
- q.push(y);
- }
- //第三个方向 2*x
- y.num = x.num * ;
- if (judge(y.num)){
- visit[y.num] = ; //标记该点已访问
- q.push(y);
- }
- }//while(q)
- }
- int main()
- {
- while (scanf("%d%d", &n, &k) == ){
- bfs();
- }
- return ;
- }
HDU 2717 Catch That Cow --- BFS的更多相关文章
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- hdu 2717 Catch That Cow(BFS,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
随机推荐
- LightOJ 1047-Program C
Description The people of Mohammadpur have decided to paint each of their houses red, green, or blue ...
- MJPhotoBrowser 两个bug:回到小图模式时会闪动&大图太靠近底部
最近项目需要写网络的相片视频浏览的库, 没时间重写,使用了MJPhotoBrowser,里面的一些bug 和解决写在下面 1.-[MJPhotoLoadingView setProgress:]: m ...
- @ResultMapping注解
@RequestMapping注解1.url映射放在方法上:@RequestMapping("/itemsEdit")2.窄化url请求映射放在类上,定义根路径,url就变成根路径 ...
- php的数组与数据结构
一.数组的分类与定义 分类: 1.索引数组 $array = array(1,2,3,4,5); 2.关联数组 $array=array(1=>"aa","bb ...
- Android常见控件— — —TextView
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...
- hdu4417 划分树+二分
//Accepted 14796 KB 453 ms //划分树 //把查询的次数m打成n,也是醉了一晚上!!! //二分l--r区间第k大的数和h比较 #include <cstdio> ...
- hdu 1069
//Accepted 264 KB 0 ms //每种block只有三种方法,且每种放法至多放一次 //规定三条边的顺序后 //把所有的block按x递增排序,x相同则按y递增排序 //然后dp // ...
- detangle c++ symbols
hust$ c++filt _ZN1AC2Ev hust$A::A()
- http请求利器: 今天配置出了RESTClient,用MAVEN构建了UI运行包
- Swift:函数和闭包
函数 函数是一个完成独立任务的代码块,Swift中的函数不仅可以像C语言中的函数一样有函数的参数和返回值,而且还支持嵌套,并且有函数参数默认值.可变参数等. //定义一个函数,注意参数和返回值,如果没 ...