题目: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. mysql常用数据类型的选择

    时间戳可以用int来存储 ip地址的存储数据类型,可以使用INET_ATON 和INET_NTOA来配合bigint类型来代替varchar

  2. easy ui datagrid 数据绑定

    1.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  3. Who needs an architect?---Martin Fowler

    英文及译文下载链接:http://pan.baidu.com/share/link?shareid=163291504&uk=1428554614 1.文章主题总结 首先我们从文章的几个小标题 ...

  4. Schtasks 命令详解

    管理计划任务 SCHTASKS /parameter [arguments] 描述:     允许管理员创建.删除.查询.更改.运行和中止本地或远程系统上的计划任务. 参数列表:     /Creat ...

  5. UILabel设置行间距和字间距并计算高度-b

    #define UILABEL_LINE_SPACE 6 #define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height //给UILabe ...

  6. iOS 状态栏管理

    iOS 7 以前:状态栏由 UIApplication 管理 1.隐藏状态栏 : application.statusBarHidden = NO; 2.设置状态栏样式 : application.s ...

  7. JSP页面用EL表达式 输出date格式

    JSP页面用EL表达式 输出date格式 1.头上引入标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix ...

  8. nginx+ tomcat集群+动静资源分离

    不知道为什么这个随便删不掉,写了也值显示一半一半不显示, 我把重新写了一遍: nginx + tomcat集群和动静资源分离

  9. Spring MVC控制层的返回类型--String类型与Bean类型

    SpringMVC控制层的返回类型形式多样,现拿其中的两种--String类型与Bean类型作以说明. 一.测试项目的结构 说明:(jsp的名字没起好) 控制层:UserController.java ...

  10. PAT-乙级-1038. 统计同成绩学生(20)

    1038. 统计同成绩学生(20) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求读入N名学生的成绩,将 ...