进一步了解了bfs;

题意:给你n,然后用+,-,*三种运算使n变成k;

漏洞:在算出新的数字之后,一定要判边界,否则RE,而且在每一步后面都得加判断是否等于K,如果是即刻退出,否则WA,判这个的时候需要顺序;

不过不明白为什么bfs这两个顺序为啥结果不同

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 101000
using namespace std;
int d[],vis[];
int k,n;
int OK(int t)
{
if(t < || t > N)
return ;
return ;
}
int bfs()
{
queue<int> q;
q.push(n);
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
d[n] = ;
vis[n] = ;
int s;
while(!q.empty())
{
int t = q.front();
q.pop();
// if(s == k)///满足条件才能到这一条
// return d[s];
for(int i=; i<; i++)
{
if(i==) s = t + ;
if(i==) s = t - ;
if(i==) s = t * ;
if(OK(s)&&!vis[s])
{
vis[s] = ;
q.push(s);
d[s] = d[t] + ;
}
else
continue;
if(s == k)///满足条件才能到这一条,放在上边就错了,为啥啊
return d[s];
}
}
return -;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n>=k)
cout<<n-k<<endl;
else
cout<<bfs()<<endl;
}
return ;
}

POJ 3278 经典BFS的更多相关文章

  1. poj 3278 简单BFS

    题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1.-1.*2三种移动方式,问最少需要多少分钟抓住奶牛 AC代码: #include<cstdio> #include<cstrin ...

  2. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  3. BFS POJ 3278 Catch That Cow

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

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

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

  5. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  6. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

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

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

随机推荐

  1. MySQL 存储php中json_encode格式中文问题及解决

    MySQL 存储php中json_encode格式信息  ,遇到中文时, 会变成一堆类似uxxxx信息. 1. 原因分析:在存储到数据库时!MySQL 不会存储 unicode 字符: MySQL 仅 ...

  2. debug && release

    http://www.cnblogs.com/awpatp/archive/2009/11/05/1597038.html Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调 ...

  3. express+nodecoffee写passport登录验证实例(二)

    二:实现登录认证 passport官网文档:  http://passportjs.org/guide/ passport验证使用一种被称为“策略”的方式来验证请求,策略支持3种类型的验证:用户名密码 ...

  4. Mysql 字符串处理函数

    函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...

  5. mysql str_to_date字符串转换为日期

    mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期. 示例:分隔符一致,年月日要一致 select str_to_date('2008-4-2 15:3:28','% ...

  6. [转]Visual Studio 2010 单元测试目录

    Visual Studio 2010 单元测试共分七个部分: 普通单元测试.顺序单元测试.压力测试,Generic测试.数据库测试.UI界面测试和Web性能测试. 这个系列的博客实例程序可以在下面的链 ...

  7. case when的用法

    国家(country)人口(population)           中国600            美国100            加拿大100            英国200       ...

  8. 牛场围栏(vijos 1054)

    题目大意: 给出N种木棍(每种木棍数量无限)的长度(<=3000),每根木棍可以把它切掉[1,M]的长度来得到新的木棍. 求最大的不能被组合出来的长度. 如果任何长度都能组合出来或者最大值没有上 ...

  9. Zooming MKMapView to fit annotation pins

    http://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins - (MKCoordinateR ...

  10. C++-模板的声明和实现为何要放在头文件中

    源: http://blog.csdn.net/lqk1985/archive/2008/10/24/3136364.aspx 如何组织编写模板程序 发表日期: 1/21/2003 12:28:58 ...