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 ...
随机推荐
- Codeforces Round #199 (Div. 2) B. Xenia and Spies
B. Xenia and Spies time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 黑马程序猿_Objective C 类与协议
<a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>.& ...
- Bitmap Style Designer非官方说明
Bitmap Style Designer Bitmap Style Designer给我的第一印象就是简陋,估计也是为了赶工.大致体会了一下,还是能够使用.因为目前没有对此有比较详细的中文资料,就把 ...
- poj3278Catch That Cow(BFS)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37094 Accepted: 11466 ...
- 关于android的nfc问题
最近在研究android的nfc问题 首先再网上有很多关于android nfc 读写的问题,但是大多数都是关于Mifare Classic类型卡的读写,我百试不得骑解,于是自己写了一些程序供大家参考 ...
- net core与golang web
Asp.net core与golang web简单对比测试 最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试. 下面是测试环境: CPU:E3-1230 ...
- eclipse之The currrently displayed page contains invalid values错误
现象: eclipse的preferences里面须要保存密码,保存报错Could Not Accept ChangesThe currrently displayed page contains i ...
- 解决Java compiler level does not match the version of the installed Java project facet.问题
其实之前遇到过Java compiler level does not match the version of the installed Java project facet.这个问题,因为当时没 ...
- 关于WM_ERASEBKGND和WM_PAINT的深刻理解
一直以来,对于WM_PAINT和WM_ERASEBKGND消息不是很清楚,从书上和网上找了很多资料,大体上有以下几点说法:1>WM_PAINT先产生,WM_ERASEBKGND后产生 2.WM_ ...
- 积累的VC编程小技巧之列表框
1.列表框中标题栏(Column)的添加 创建一个List Control,其ID为IDC_LIST,在其Styles属性项下的View项里选择Report.Align项里选择Top.Sort项里选择 ...