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. Tomcat控制台中文乱码

    参考:https://blog.csdn.net/zhaoxny/article/details/79926333 1.找到${CATALINA_HOME}/conf/logging.properti ...

  2. neo4j源码分析1-编译打包启动

    date: 2018-03-22 title: "neo4j源码分析1-编译打包启动" author: "邓子明" tags: - 源码 - neo4j - 大 ...

  3. liunx驱动----信号量的实现

    使用信号量必须包含  <asm/semaphore.h>  头文件.其中相关结构体  struct semaphore 可以通过以下几种方式来声明或者初始化. 1.void sema_in ...

  4. Fusioncharts图表常用参数设置

    1.1 <chart>参数设置: 图表和轴的标题* caption=”String” : 图表上方的标题* subCaption=”String” : 图表上方的副标题* xAxisNam ...

  5. CentOS7部署CDH6.0.1大数据平台

    Cloudera’s Distribution Including Apache Hadoop,简称“CDH”,基于Web的用户界面,支持大多数Hadoop组件,包括HDFS.MapReduce.Hi ...

  6. 机器学习-KNN算法详解与实战

    最邻近规则分类(K-Nearest Neighbor)KNN算法 1.综述 1.1 Cover和Hart在1968年提出了最初的邻近算法 1.2 分类(classification)算法 1.3 输入 ...

  7. git上传代码到code.csdn.net

    国内有code.csdn.net速度很快 用git上传需要去下载git程序 很简单. 我是Windows下 先code.csdn.net创建一个项目 https://code.csdn.net/das ...

  8. vue + nginx部署404

    记录: vue项目打包放到服务器,使用nginx反向代理的时候路由页面刷新报404,因为刷新时,不存在路径对应的文件或文件夹,需要在nginx配置中添加代码: index index.html; tr ...

  9. MySQL 5.7原生通用二进制格式安装包安装过程

    目录 官方文档 文件说明 官方文档 https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html 文件说明 Directory Co ...

  10. 使用pycharm编写自动化脚本

    1.:导入本地Python环境 2:安装Selenium 3:下来需要根据安装的浏览器版本,下载合适的chromedriver驱动程序. 4:接着需要将下载的chromedriver进行解压,并将文件 ...