题目: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)的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  2. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  3. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  4. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  5. 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 ...

  6. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  7. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 浅谈String类型

    首先,我们要知道的是String类型是一个引用类型,它的基类是Object.并且它的内容是只读的. 我们有时候经常会看到两个字符串类型,一个是“Sting”,一个是“string”.大写的String ...

  2. The 50 Most Essential Pieces of Classical Music

    1. Die Zauberflöte ("The Magic Flute"), K. 620: Overture London Philharmonic Orchestra 7:2 ...

  3. WPF容器控件

    WPF有五种容器控件,分别为Grid,Canvas,StackPanel,WrapPanel,DockPanel. Grid: 1.Height=”60”:不加“星号”表示固定的高度 2.Height ...

  4. Hibernate从入门到精通(二)Hibernate实例演示

    上篇Hibernate从入门到精通(一)JDBC简介,我们主要对JDBC进行了简单介绍和使用说明,这次我们做一个Hibernate简单实例,通过这个实例对比Hibernate和JDBC,了解Hiber ...

  5. Android Studio 单刷《第一行代码》系列 07 —— Broadcast 广播

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  6. shell复习笔记----用户管理

    $ who    可以知道系统上有多少登陆 $who |wc -l 计算用户个数 注意:|是管道符号,可以在两个程序之间建立管道(pipeline):who 的输出,成了 wc 的输入, wc 所列出 ...

  7. this.Invoke和this.BeginInvoke的区别

    private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...

  8. c++ 链接

    header.h #ifndef HEADER_H #define HEADER_H unsigned long getFac(unsigned short num); ; #endif // HEA ...

  9. 支付宝Unity

    原地址:http://blog.csdn.net/sgnyyy/article/details/20444627 说明:支付宝Android的SDK接入只有一个接口,付费. 1. Android代码的 ...

  10. 深入浅出ShellExecute

    Q: 如何打开一个应用程序? ShellExecute(this->m_hWnd,"open","calc.exe",""," ...