题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k

可以先画出样例的部分状态空间树

可以知道搜索到的深度即为所需要的最小的变化次数

下面是学习的代码----@_@

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 100005
using namespace std;
queue<int> q;
int visit[maxn],step[maxn],n,k; int bfs(int n,int k)
{
int head,next,i;
q.push(n);
visit[n]=1;
step[n]=0;
while(!q.empty())
{
head=q.front();
q.pop();
for(i=1;i<=3;i++)
{
if(i==1) next=head-1;
else if(i==2) next=head+1;
else next=2*head;
if(next<=100005&&next>0)
{
if(!visit[next])//判重
{
q.push(next);
visit[next]=1;
step[next]=step[head]+1;
}
}
if(next==k) return step[next];
}
}
}
int main()
{
scanf("%d %d",&n,&k);
if(n>=k) printf("%d\n",n-k);
else
printf("%d\n",bfs(n,k));
}

  学习BFS的第一题————go---go--

补写一个模拟队列写的,还是学习的代码,参看的这一篇http://blog.csdn.net/freezhanacmore/article/details/8168265

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100005
using namespace std;
int vis[maxn],n,k;
struct node
{
int x,step;
} q[maxn];
int bfs(int n,int k)
{
node now,next;
int head=0,tail=0;
q[tail].x=n;//将当前这一节点入队
q[tail].step=0;tail++;
vis[n]=1;
while(head<tail)
{
now=q[head];//取出队头
head++;//弹出队头
for(int i=0;i<3;i++)
{
if(i==0) next.x=now.x+1;
else if(i==1) next.x=now.x-1;
else next.x=now.x*2;
if(next.x<0||next.x>=maxn) continue;//越界了的话就剪枝
if(!vis[next.x])
{
vis[next.x]=1;
next.step=now.step+1;
q[tail]=next;
tail++;
if(next.x==k) return next.step;
}
}
}
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF)
{
memset(vis,0,sizeof(vis));
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",bfs(n,k));
}
}

  

POJ 3278 Catch That Cow【BFS】的更多相关文章

  1. POJ - 3278 Catch That Cow 【BFS】

    题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...

  2. POJ 3278 Catch That Cow(bfs)

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

  3. hdoj 2717 Catch That Cow【bfs】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  5. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

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

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

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

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

  8. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

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

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

随机推荐

  1. Safari new Date() 兼容问题

    我的时间 var myTime = "2015-12-31 12:10:21"; 正常写法 var  newTime =  new Date(myTime); safari兼容写法 ...

  2. canvas绘制三等分饼型图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. (转载) listview实现微信朋友圈嵌套

    listview实现微信朋友圈嵌套 标签: androidlistview 2016-01-06 00:05 572人阅读 评论(0) 收藏 举报  分类: android(8)  版权声明:本文为博 ...

  4. pgpool如何对数据库节点进行状态检查及相关数据结构描述

    /* * configuration parameters */typedef struct {    char *listen_addresses;            /* hostnames/ ...

  5. C++函数的高级特性——小结

    相对于C语言,C++增加了重载(overload).内联(inline).const和virtual四种新机制. 1 重载 只能靠参数列表而不能紧靠返回值类型的不同来区分重载函数.编译器根据参数列表为 ...

  6. Pyhton学习——Day7

    ##############################################匿名函数################################################## ...

  7. 注解实战aftersuite和beforesuite

    package com.course.testng;import org.testng.annotations.*; public class BasicAnnotation { //最基本的注解,用 ...

  8. git diff patch方法

    UNIX世界的软件开发大多都是协作式的,因此,Patch(补丁)是一个相当重要的东西,因为几乎所有的大型UNIX项目的普通贡献者,都是通过 Patch来提交代码的.作为最重要的开源项目之一,Linux ...

  9. js Math常用方法

    ------------------------ 向上取整,有小数就整数部分加1 Math.ceil(5/2) ------------------------ 四舍五入. Math.round(5/ ...

  10. HDU——T The King’s Problem

    http://acm.hdu.edu.cn/showproblem.php?pid=3861 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...