题意:可以把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. [Aaronyang] 写给自己的WPF4.5 笔记16[多线程]

      =============潇洒的版权线==========www.ayjs.net===== Aaronyang ========= AY =========== 安徽 六安 杨洋 ======= ...

  2. 搞定所有的跨域请求问题 jsonp CORS

    网上各种跨域教程,各种实践,各种问答,除了简单的 jsonp 以外,很多说 CORS 的都是行不通的,老是缺那么一两个关键的配置.本文只想解决问题,所有的代码经过亲自实践.   本文解决跨域中的 ge ...

  3. android 开发小工具收集

    http://blog.csdn.net/tikitoo/article/details/51089422

  4. 音视频编解码: YUV采样格式中的YUV444,YUV422,YUV420理解

    YUV各种采样格式的说明 通常我们用RGB表示一种彩色.计算机系统里的LCD显示的数据就是RGB来表示每个像素的颜色.而在我们生活里,有黑白电视机与彩色电视机两种,拍摄节目源时不可以用两种不同的摄像机 ...

  5. docker打包centos增加中文支持

    docker打包centos增加中文支持 前言 使用的某个包的返回值,在本机测试时返回结果是中文,结果打包到docker后返回结果变英文了:猜测是系统语言的问题,进入docker测试了一下,发现果然是 ...

  6. Tomcat是什么:Tomcat与Java技、Tomcat与Web应用以及Tomcat基本框架及相关配置

    1.Tomcat是什么       Apache Tomcat是由Apache Software Foundation(ASF)开发的一个开源Java WEB应用服务器. 类似功能的还有:Jetty. ...

  7. ios UIButton设置高亮状态下的背景色

    一,通过按钮的事件来设置背景色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 - (void)viewDidLoad {     [ ...

  8. 我在tmux中最不可少的配置: 用鼠标切换窗口/调节分屏大小

    前两天在给另外一个团队帮忙时,看他们在Rails日志.代码文件.git文件系统里面来回穿梭,觉得他们太累了,于是就介绍了 tmux 给他们用.但只讲了一点基本的开窗口.分屏,没给讲太多技巧,因为一下子 ...

  9. centos7安装elasticsearch-head

    elasticsearch-head安装前准备 1.操作系统64位CentOS Linux release 7.2.1511 (Core)2.git是必需的elasticsearch-head是一款开 ...

  10. headfirst python 03, 04

    文件与异常 python中的输入机制是基于行的, open()函数与for 语句结合使用, 可以非常容易的读取文件.(打开->处理->关闭) #!/usr/bin/env python # ...