poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278
题意:
给定两个整数n和k
通过 n+1或n-1 或n*2 这3种操作,使得n==k
输出最少的操作次数
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[];
struct node
{
int x,step;
};
int bfs(int n,int k)
{
if(n==k)
return ;
queue<node>q;
struct node next,pos;
next.step=; next.x=n;
vis[n]=;
q.push(next);
while(!q.empty())
{
next=q.front();
q.pop();
if(vis[next.x-]==&&(next.x-)>=&&(next.x-)<=)
{
pos.x=next.x-; pos.step=next.step+;
q.push(pos);
vis[next.x-]=;
if(pos.x==k)
return pos.step;
}
if(vis[next.x+]==&&(next.x+)>=&&(next.x+)<=)
{
pos.x=next.x+; pos.step=next.step+;
q.push(pos);
vis[next.x+]=;
if(pos.x==k)
return pos.step;
}
if(vis[next.x*]==&&(next.x*)>=&&(next.x*)<=)
{
pos.x=next.x*; pos.step=next.step+;
q.push(pos);
vis[next.x+]=;
if(pos.x==k)
return pos.step;
}
}
return ;
};
int main()
{
int n,k,sum;
while(~scanf("%d%d",&n,&k))
{
memset(vis,,sizeof(vis));
sum=bfs(n,k);
printf("%d\n",sum);
}
}
poj 3278 Catch That Cow (bfs)的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 3278 Catch That Cow(BFS 剪枝)
题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...
- POJ——3278 Catch That Cow(BFS队列)
相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- 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 Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
随机推荐
- @+id/和android:id的区别
android:id="@+id/btn",表示在R.java文件里面新增一个id为btn的控件索引,最常用的一种声明控件id的方式.android:id="@andro ...
- jQuery 图片轮播的代码分离
以前遇到过jQuery实现列表自动滚动,这次的图片轮播在原理上与之相同,只有一些细微的差别,就是需要在图片的右下角显示当前图片的序号. html代码,以及对应的css代码: <div id=&q ...
- 提升PHP性能的21种方法
提升PHP性能的21种方法. 1.用单引号来包含字符串要比双引号来包含字符串更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会.2.如果能将类的方法定义成static,就尽量定义成st ...
- php连接ftp
PHP连接ftp,发现一个很好用的类库phpseclib.英文原文 Connecting to SFTP with PHP If you need to connect to SFTP using P ...
- SharedPreference.Editor的apply与commit方法不同之处
定义: void apply boolean commit; 相同:二者都是提交修改的数据 手机里的文件存放在/data/data/<package_name>/shared_prefs ...
- Keil V4.72升级到V5.1X之后
问题描述 Keil V4.72升级到V5.1x之后,原来编译通过的工程,出现了如下错误: .\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\STM32f ...
- (转)sqlserver游标概念与实例全面解说
首先声明:该文章转自http://www.cnblogs.com/wudiwushen/archive/2010/03/30/1700925.html 的博客 引言 我们先不讲游标的什么概念,步骤 ...
- SVN弱密码扫描(Python)
寂寞如雪的用脑过度,所以来写个博客分享一下.#虽然上一篇博客我还没写完 SVN的弱密码,看起来很复杂,但实际上很简单啊= =虽然不像pymssql/mymssql这种,Python提供了很好用的包,但 ...
- 机器学习基石的泛化理论及VC维部分整理(第五讲)
第五讲 Training versus Testing 一.问题的提出 \(P_{\mathcal{D}}\left [ BAD \mathcal{D} \right ] \leq 2M \cd ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...