POJ 3278 经典BFS
进一步了解了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的更多相关文章
- poj 3278 简单BFS
题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1.-1.*2三种移动方式,问最少需要多少分钟抓住奶牛 AC代码: #include<cstdio> #include<cstrin ...
- 【BFS】POJ 3278
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 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 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(模板——BFS)
题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...
随机推荐
- swfobject2.2
官方网址:http://blog.deconcept.com/swfobject/ Github地址:https://github.com/swfobject/swfobject 谷歌地址 貌似被和谐 ...
- 51nod 1065 最小正子段和
题目链接:51nod 1065 最小正子段和 房教说用前缀和做,然后看了别人博客懂了后就感觉,这个真有意思... #include<cstdio> #include<cstring& ...
- SQL Server数据库(作业讲解和复习)
--第一题 查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from student --第二题 查询教师所有的单位即不重复的De ...
- for循环的嵌套——7月24日
练习一:输入一个正整数,用for循环嵌套求阶乘的和 //输入一个正整数,求1!+2!+....+n! 用for循环嵌套 Console.Write("请输入一个正整数:"); ...
- lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为一行
如何联合索引查询? 所以给定查询过滤条件 age=18 的过程就是先从term index找到18在term dictionary的大概位置,然后再从term dictionary里精确地找到18这个 ...
- php中curl和fsockopen发送远程数据的应用
最近要用到通过post上传文件,网上盛传的有curl的post提交和fsockopen,其中curl最简单,于是从最简单的说起. 这是简单的将一个变量post到另外一个页面 $url = ''; $d ...
- java语句类型
public class Test { public static void main(String[] args) { System.out.println("Test is ok&quo ...
- autoLyout纯代码适配
前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...
- zoj2132-The Most Frequent Number
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2132 The Most Frequent Number Time Limi ...
- java日期的运用(DateUtils工具类)
public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...