POJ - 3278
题目链接:http://poj.org/problem?id=3278
ac代码:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int a,b;
int vis[100005];
queue<int> q;
int bfs(){
if(a>=b) return a-b;
int x;
q.push(a);
vis[a] = 0;
while(!q.empty()){
int t = q.front();q.pop();
if(t==b) return vis[t];
for(int i = 0;i<3;i++){
if(i==0) x = t + 1;
else if(i==1) x = t - 1;
else x = t*2;
if(x<0||x>100005) continue;
if(vis[x]==-1){
vis[x] = vis[t] + 1;
q.push(x);
}
}
}
}
int main(){
while(scanf("%d%d",&a,&b)!=EOF){
while(!q.empty()) q.pop();
memset(vis,-1,sizeof(vis));
printf("%d\n",bfs());
}
}
POJ - 3278的更多相关文章
- 【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 ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
随机推荐
- [Java Web学习]junit.framework.AssertionFailedError: No tests found in {Class}
No tests found in com.XXXXX.XXX.inboundPrepService.bizLogic.prepDeterminationEngine.workers.Determin ...
- qt+opencv LNK4272:library machine type 'x64' conflicts with target mathine type 'x86'
运行时报错如上图所示,原因是你使用的opencv库是64位的,qt里面使用的编译器MSVC是32位的,解决方法如下: 将构建套件修改位64bit:
- ML_Clustering
西瓜书学习...ing K均值 k-means 给定样本集$ D = {X_1,X_2,...X_n} \(,k-means针对聚类所得簇划分\)C = {C_1,C_2,...,C_k}$最小化平方 ...
- 剑指offer 7. 递归和循环 斐波那契数列
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 简简单单 废话不多说,直接上代码: public class Sol ...
- jquery 一键复制文本到剪切板
<a id="copy" data-clipboard-text="123456">复制文本</a> $(function(){ var ...
- PHP中的反射模拟框架中控制器的调度
<?php class IndexAction { public function index() { echo 'index'; } public function indexBefore() ...
- Ubuntu16.04下安装nginx+mysql+php+redis
一.redis简介 Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记 ...
- [转摘]VMware下Windows系统出现大量可删除ATA Channel的解决办法
编辑VMX配置文件加上一句话就可以了 devices.hotplug = "false" 原文:http://blog.ihipop.info/2015/05/4830.html
- 由consequence忽然发现英语也挺有意思
con- 是拉丁语前缀, 有 with, together 的意思. con- 和 com- 一样的. 只是因为 在 b p m 前发 m 音更方便, 所以这些音前的 con- 变为 com- (例 ...
- CentOS7用hostapd做radius服务器为WiFi提供802.1X企业认证
CentOS7用hostapd做radius服务器为WiFi提供802.1X企业认证 来源: https://www.cnblogs.com/osnosn/p/10593297.html 来自osno ...