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不能超过最大值. ...
随机推荐
- FG模型
一直没搞懂CvBGStatModel和CvFGDStatModel有什么区别.CvBGStatModel模型的创建用cvCreateGaussianBGModel,CvFGDStatModel模型的创 ...
- opencv实现图片缩放
源码 #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/img ...
- php的查询数据
php中 连接数据库,通过表格形式输出,查询数据.全选时,下面的分选项都选中;子选项取消一个时,全选按钮也取消选中. <!DOCTYPE html PUBLIC "-//W3C//DT ...
- html练习——个人简介
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C杂记
printf主要是为了便于取地址,默认将char,short变成了int,float变成了double:但scanf的时候,因为数据宽度的问题,必须区分%f和%lf. memset 是字符串操作函数: ...
- hdoj-2023
#include "stdio.h"int main(){ int a[55][6]; double pingjun[55],mk[6]; int n,m,i,j,sum=0,co ...
- 4 Values whose Sum is 0_upper_bound&&ower_bound
Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer val ...
- HDU 4869 (递推 组合数取模)
Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...
- CODEVS1380 没有上司的舞会 (树形DP)
f[i,0] 表示 第i个人不参加舞会 f[i,1] 表示 第i个人参加舞会 f[i,1]=sigma(f[j,0])+v[i] j 为 i 的孩子 f[i,1]=sigma(max(f[j,0] ...
- HDU 4850
http://acm.hdu.edu.cn/showproblem.php?pid=4850 西安邀请赛当时没做出来的银牌门坎题 题意:构造一个长度n的字符串,长度>=4的子串只能出现一次 题解 ...