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不能超过最大值. ...
随机推荐
- java中的日志组件-log4j
1.为什么使用日志组件 Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT的事件记录器:还可以控制每一 ...
- powershell加win的dns服务器,解决网站负载均衡问题
用我发明的powershell填坑法,加windows的dns服务器.从调整dns服务器解析ip时间段的角度,解决网站负载均衡问题. ------------------------win2012r2 ...
- 初识VBS
做了测试快一年了吧,迫于无奈,要学习自动化的只是,首先想到了QTP,但是QTP的脚本是VBS,所以必须要会VBS. VBS其实就是一门计算机编程语言,但是缺少计算机程序语言中的部分要素,对于事件的描述 ...
- Installing Cygwin and Starting the SSH Daemon
This chapter explains how to install Cygwin and start the SSH daemon on Microsoft Windows hosts. Thi ...
- C# 启动关闭.exe进程(转)
后台代码: 1 using System.Diagnostics; 2 3 protected void Button1_Click(object sender, EventArgs e)4 ...
- Ajax方法实现登录页面
Note: ajax技术 不用刷新页面,做局部刷新不用form表单,因为不需要提交,通过JQuery控制必须要有id如果要用ajax可以用JQuery也可以用js写,推荐JQuery 因为简单,直接引 ...
- unity3d角色控制器01
参考出处貌似是雨松大神.如有侵权,立即删除. 需要导入包 ①将FirstPerson Controller拖拽入Hierarchy(层次视图)中.由于角色控制器是具有一定物理引擎的,所以一定要将它放在 ...
- (转)innerHTML、innerText和outerHTML、outerText的区别
原文:http://walsh.iteye.com/blog/261966 innerHTML.innerText和outerHTML.outerText的区别 博客分类: CSS/ ...
- 2016 - 1 -17 GCD主队列与全局队列
一:主队列 1.概念:每一个应用程序对应唯一一个主队列,直接GET即可:在多线程开发中,使用主队列更新UI dispatch_queue_t q = dispatch_get_main_queue() ...
- Tomcat容器运行struts2+spring+mybatis架构的java web应用程序简单分析
1.具体的环境为 MyEclipse 8.5以及自带的tomcat spring3.0.5 struts2.3.15.1 mybatis3.0.5 2.想弄明白的一些问题 tomcat集成spring ...