HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717
思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值。
然后还要注意N>=K的时候,只能减1才能到达。
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <queue>
using namespace std;
struct node{
int pos,step;
}p,q;
int N,K,mmin;
bool visit[200010];
void bfs()
{
queue<node> Q;
memset(visit,0,sizeof(visit));
p.pos=N;
p.step=0;
Q.push(p);
visit[N]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(p.pos==K){
if(p.step<mmin)mmin=p.step;
}
for(int i=0;i<3;i++)
{
if(i==0){ //x+1;
if(p.pos>K)
continue;
q.pos=p.pos+1;
if(visit[q.pos])continue;
q.step=p.step+1;
Q.push(q);
visit[q.pos]=1;
}
else if(i==1){ //x-1; q.pos=p.pos-1;
if(visit[q.pos])continue;
if(q.pos<0)continue;
q.step=p.step+1;
Q.push(q);
visit[q.pos]=1;
}
else if(i==2){ //2*x;
if(p.pos>K)continue;
q.pos=2*p.pos;
if(visit[q.pos])continue;
if(q.pos>100000)continue;
q.step=p.step+1;
Q.push(q);
visit[q.pos]=1;
} }
}
}
int main()
{
//ifstream fin;
//fin.open("data1.txt");
while(cin>>N>>K)
{
mmin=99999;
if(N>=K){
cout<<N-K<<endl;
}
else {
bfs();
cout<<mmin<<endl;
} } return 0; }
HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行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/Oth ...
- 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广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 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)
题目链接: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 (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants 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 ...
随机推荐
- C++中实现 time_t, tm 相互转换
time_t -> tm: localtime tm -> time_t: mktime time_t curTime; time(&curTime); dwCurTime = c ...
- Ajax技术——带进度条的文件上传
1.概述 在实际的Web应该开发或网站开发过程中,经常需要实现文件上传的功能.在文件上传过程中,经常需要用户进行长时间的等待,为了让用户及时了解上传进度,可以在上传文件的同时,显示文件的上传进度条.运 ...
- javascript 剔除数组中相同的值,合并数组中相同项
var a = ["2013-01","2013-01","2013-02","2013-02","2013- ...
- 杭电ACM1408——盐水的故事
简单的题目,RT,就能够写出代码.须要注意的是类型的应用,应该用浮点型. 以下的是AC的代码: #include <iostream> using namespace std; int m ...
- Python的字符串格式化符号
1.字符串格式化符号含义 %c:格式化字符及其ASCII码 %s:格式化字符串 %d:格式化整数 %o:格式化无符号八进制数 %x:格式化无符号十六进制数 %X:格式化无符号十六进制数(大写) %f: ...
- XML 关键字
SGML--Standard Generalized Marked Language 标准通用标记语言GML--Generalized Marked Language 通用标记语言XML--Extes ...
- sum(case when then)(男女生的个数)
判断类似一个班级的男生和女生的人数,用sum (cese when then ) select count(er.execute_result), sum(case er.execute_result ...
- hdu 1760 一道搜索博弈题 挺新颖的题目
A New Tetris Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Python中的列表解析和生成表达式
摘要:优雅.清晰和务实都是python的核心价值观,如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析( List comprehensions)和生成表达式,通过 ...
- javascript——ajax应用
概念 AJAX 指异步JavaScript及XML(Asynchronous JavaScript And XML).Ajax的核心是JavaScript对象XmlHttpRequest.XmlHtt ...