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 ...
随机推荐
- hdu----149850 years, 50 colors(最小覆盖点)
50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- kafka consumer属性
consumer属性 group.id:指定consumer group的唯一标识. consumer.id:唯一标识consumer.默认值为null,不指定时会自动生成. zookeeper.co ...
- BestCoder Round #11 题解集合
1001.Alice and Bob 签到题*1,只要x * 2 == n && y * 2 == m就满足条件. var m, n, x, y : int64; begin whil ...
- select option居中显示
<style> .ch-select{ padding:0px;} .ch-select input[type=text]{ width:100%; position:relative; ...
- 一个Java递归程序
先来没事搜了一些面试题来做,其中一道:输入一个整数,求这个整数中每位数字相加的和? 思考:1.如何或得每一位数:假如是1234, 1234%10=4,得到个位:(1234/10)%10=3得到十位 ...
- 查看Nginx、apache、MySQL和PHP的编译参数
1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# cat /usr/local/apache/build/config.nice3.p ...
- PHP+socket游戏数据统计平台发包接包类库
<?php /** * @title: PHP+socket游戏数据统计平台发包接包类库 * @version: 1.0 * @author: perry <perry@1kyou.com ...
- Threading.Tasks.Task多线程 静态全局变量(字典) --只为了记录
--------------------------------------------------------------后台代码---------------------------------- ...
- 浏览器渲染原理--reflow
Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都是 ...