题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数。

坑:标题写了。有点不会写bfs了。。。

ac代码

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
const int maxn = 1e5 + ;
int vis[maxn];
struct node {
int x, t;
node(int n = , int k = ) :x(n), t(k) {}
}; int n, k;
bool check(int x) {
if ( x > 1e5 || x < || vis[x])return ;
else return ;
}
void Run() {
queue<node> Q;
Q.push(node(n,));
vis[n] = ;
while (!Q.empty()) {
node now = Q.front();
Q.pop(); if (now.x == k) { cout << now.t; break; }
if (check(now.x + ))Q.push(node(now.x + , now.t + )),vis[now.x + ] = ;
if (check(now.x - ))Q.push(node(now.x - , now.t + )), vis[now.x - ] = ;
if (check(now.x * ))Q.push(node(now.x * , now.t + )), vis[now.x * ] = ; }
} void smain() { cin >> n >> k; Run(); }

Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。的更多相关文章

  1. catch that cow POJ 3278 搜索

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

  2. (广搜)Catch That Cow -- poj -- 3278

    链接: http://poj.org/problem?id=3278 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6211 ...

  3. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  4. C - Catch That Cow POJ - 3278

    //标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...

  5. kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278

    题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...

  6. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

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

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

  8. Catch That Cow (简单BFS+剪枝)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  9. POJ:3259-Wormholes(最短路判断负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58153 Accepted: 21747 Descripti ...

随机推荐

  1. IDEA使用笔记(六)——设置项目的JDK配置

    1:由于dev分支和master分支的代码差异比较多,所以,就从master上分出一个新的分支dev_,于是我就克隆新的代码,打开对应的项目文件,然后启动试试,发现报出如下的错误,很明显是因为没有制定 ...

  2. SNF快速开发平台MVC-自由排序组件

    1.    自由排序功能使用 在一些需要排序优先级的数据进行调整处理,如民族数据,在北方实施的时候汉族比较多,希望把汉族放在第一位.在蒙古实施项目时,蒙古族人最多把蒙古族放在第一选择位. 1.1.   ...

  3. 如何保留自己对eclipse的个性化设置,在其他工程中使用

    有的个性化设置是在功能的.metadata\.plugins文件夹下保存.所以新建功能后,个性化设置都没有了. 解决办法: 新建一个工程后,调好个性化配置.然后备份整个工程. 以后不要新建工程,直接解 ...

  4. Activity标题(title)的显示和隐藏

    开发Android应用程序,我们总会遇到Activity的title把显示的内容遮挡了一部分.如果能把它去掉,我们的应用界面就会变得更加简洁,那该多好.下面有两种方法可以去掉: (方法一):通过一句J ...

  5. [docker]docker4种网络最佳实战

    参考: http://hicu.be/docker-container-network-types docker默认3中网络类型 参考: https://docs.docker.com/engine/ ...

  6. python文件的基础操作

    import os print(,'-')) print(os.getcwd()) print(,'-')) print(os.listdir()) print(,'-')) print(os.lis ...

  7. Qt库版查询

    1 背景 在为嵌入式产品开发Qt应用时,开发所使用的Qt库要和嵌入式系统所支持的Qt库版本一致,否则开发的App无法正确运行.那么,如何查询一个嵌入式系统中所安装Qt库的版本呢?下面将进行一些总结. ...

  8. oracle转mysq遇到的问题--------1.nextval问题

    oralce里面有一个问题,在做采购单模块时,遇到如下问题: 所以我们要解决这个问题: 第一步: 并发生成方式: sequence的并发生成方式同非并发生成方式都需要建立sequence表,如下: 一 ...

  9. 嵌入式开发之hi3519---GPIO 按键驱动

    摸索了一个星期,终于把海思HI3515开发板的按键中断程序搞出来了,hi3515的核心芯片与网上例子较多的s3c之类的有一些区别,以至于浪费了好些时间去琢磨.管脚配置方式不一样,中断的使用情况也不一样 ...

  10. Spark学习笔记——手写数字识别

    import org.apache.spark.ml.classification.RandomForestClassifier import org.apache.spark.ml.regressi ...