1poj 3278

http://poj.org/problem?id=3278

#include<math.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iostream> using namespace std;
#define N 100005
#define M 155
#define INF 0x3f3f3f3f
struct node
{
int x, t;
}st;
int n, k, a, b, x, y, t, cnt, ans;
bool v[N]; void Bfs(int n)
{
ans = 0;
queue<node>q;
while(!q.empty()) q.pop();
memset(v, 0, sizeof(v));
st.x = n;
st.t = 0;
q.push(st);
v[st.x] = 1; while(!q.empty())
{
node head = q.front();
q.pop();
if(head.x == k)
{
ans = head.t;
return;
} for(int i = 0; i < 3; i++)
{
node next = head;
if(i == 0) next.x = head.x + 1;
else if(i == 1) next.x = head.x - 1;
else next.x = head.x * 2; next.t = head.t + 1; if(next.x < 0 || next.x > 100000) continue;
if(v[next.x] == 1 ) continue;
// 一开始写成一行,if(v[next.x] == 1 || next.x < 0 || next.x > 100000) continue; 找了好久的bug,以后能分开的还是分开吧,不要省那一两行
q.push(next);
v[next.x] = 1;
}
}
} int main()
{
while(cin>>n>>k){ Bfs(n); cout<<ans<<endl; }
return 0;
}

裸BFS题若干的更多相关文章

  1. USACO 3.2 msquare 裸BFS

    又是个裸BFS... 和西安网赛那道1006一样的,只不过加上了要记录方案.顺便复习map 记录方案直接在bfs队列的结点里加一个vector<int> opt,把从开头一直到当前结点的操 ...

  2. 一道很经典的 BFS 题

    一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...

  3. D. Kilani and the Game 解析(裸BFS、實作)

    Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...

  4. HDU 1728 逃离迷宫 BFS题

    题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...

  5. POJ 水题若干

    POJ 3176 Cow Bowling 链接: http://poj.org/problem?id=3176 这道题可以算是dp入门吧.可以用一个二维数组从下向上来搜索从而得到最大值. 优化之后可以 ...

  6. HDU1372:Knight Moves(经典BFS题)

    HDU1372:Knight Moves(BFS)   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  7. zzuli2130卡时bfs题

    https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 2130: hipercijevi Time Limit: 1 Sec  Memory Li ...

  8. POJ-1274The Perfect Stall,二分匹配裸模板题

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23313   Accepted: 103 ...

  9. tarjan讲解(用codevs1332(tarjan的裸题)讲解)

    主要借助这道比较裸的题来讲一下tarjan这种算法 tarjan是一种求解有向图强连通分量的线性时间的算法.(用dfs来实现) 如果两个顶点可以相互通达,则称两个顶点强连通.如果有向图G的每两个顶点都 ...

随机推荐

  1. windows安装nginx部署

    转自:https://www.jb51.net/article/47066.htm 一.下载安装Nginx(本文环境为windows xp 32bit环境) 解压nginx-1.0.11.zip,进入 ...

  2. SpringMVC设置不拦截静态资源css,js

    转自:https://blog.csdn.net/sh513023410/article/details/81361867

  3. Linux Qt cannot find -lGL错误完美解决方案(亲测有效)

    http://c.biancheng.net/view/3901.html 对于很多 Linux 发行版本,Qt 安装完成后如果直接编译或者运行项目,会出现“cannot find -lGL”错误,如 ...

  4. web框架-(二)Django基础

    上面我们已经知道Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sessi ...

  5. windows 10 安装openssh 0x800f0954 的一种解决方法

    安装与卸载参考:https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_first ...

  6. python time、datetime模块

    时间的三种格式:1)时间戳 2)格式化的时间字符串 3)元组(struct_time):time.struct_time(tm_year=1970, tm_mon=5, tm_mday=23, tm_ ...

  7. DDD领域驱动设计初探(六):领域服务

    前言:之前一直在搭建项目架构的代码,有点偏离我们的主题(DDD)了,这篇我们继续来聊聊DDD里面另一个比较重要的知识点:领域服务.关于领域服务的使用,书中也介绍得比较晦涩,在此就根据博主自己的理解谈谈 ...

  8. 初学oracle第三天

    体系结构 Oracle 采取的是 Client/Server 架构. 3.1 Client   3.1.1 Sqlplus 这是一个轻量级的功能强大的客户端, 是 dba 必须掌握的工具. 我们可以配 ...

  9. 继承父类的注入Bean

    Bcontroller 继承了 Acontroller ,Acontroller注入了一个API,通过API实现了一个功能“方法X”.在Bcontroller中调用 Acontroller 的“方法X ...

  10. H5 FileReader对象

    前言:FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. < ...