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 ...
随机推荐
- Http UDP还是TCP
http://1024monkeys.wordpress.com/2014/04/01/game-servers-udp-vs-tcp/ 在编写网络游戏的时候,到底使用UDP还是TCP的问题迟早都要面 ...
- Spark Streaming揭秘 Day4-事务一致性(Exactly one)
Spark Streaming揭秘 Day4 事务一致性Exactly one 引子 对于业务处理系统,事务的一致性非常的关键,事务一致性(Exactly one),简单来说,就是输入数据一定会被处理 ...
- android.support.v7.app.AppCompatActivity
1.Android Studio (api 23) 新建项目的时候 Activity public class MainActivity extends AppCompatActivity 2.系统默 ...
- Java - 选择性排序 PHP || Java 代码对比
int [] array1 = {1,3,5,7,9,10,2,15,154,10,2,188,200};//定义一个数组,内容为混乱大小 int index = 0;//定义一个最大值或最小值的位置 ...
- 子查询优化成join关联查询时要注意一对多关系
mysql> select * from t where t.id in (select t1.tid from t1); +------+ | id | +------+ | +------+ ...
- EXTJS 4.2 日期控件
{ xtype: "fieldcontainer", layout: "hbox", items: [{ fieldLabel: '开始时间', name: ' ...
- cocos2dx之C++调用Lua
原文地址:http://blog.csdn.net/dingkun520wy/article/details/49839701 1.引入头文件 #include "cocos2d.h&quo ...
- 【BZOJ3196】Tyvj 1730 二逼平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的 ...
- sb 讲解 (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]
代码:(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]] 输出sb. 分段解析: 首先解析s: (! ...
- js的面向对象的程序设计之理解继承
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(六) 先来解析下标题——对象和继承~ 一.对象篇 ECMA-262把对象的定义为:&qu ...