poj3278 BFS

里面稍微有一个坑的地方

这个地方一定要是100001而不可以是2*k

#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 100000 + 50;
int n, k;
bool vis[maxn];
int f[maxn];queue<int> q;
int bfs()
{while (!q.empty())q.pop();vis[n] = 1;f[n] = 0;q.push(n);while (!q.empty()){int now = q.front();int next;q.pop();for (int i = 0; i < 3; i++){if (i == 0)next = now - 1;else if (i == 1)next = now + 1;else if (i == 2)next = 2 * now;if (next < 0 || next >= 100001) continue;//if (next >= 0 && next <= 2 * k)if (!vis[next]){q.push(next);f[next] = f[now] + 1;vis[next] = 1;}if (next == k)return f[next];}}}int main()
{while (cin >> n >> k){memset(vis, 0, sizeof(vis));memset(f, 0, sizeof(f));if (n < k)cout << bfs() << endl;elsecout << n - k << endl;}return 0;
}

poj3278 CatchThatCow bfs相关推荐

  1. poj3278 【BFS】

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 97240   Accepted: 30519 ...

  2. POJ3278(BFS入门)

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

  3. poj12月其他题解(未完)

    最近编程的时间比较少啊-- poj3253 就是个合并果子,各种优先队列即可(显然单调队列最优) poj3263 线段树统计每个点被覆盖了多少次即可,注意要去重 poj3625 最小生成树 poj36 ...

  4. POJ3278 HDU2717 Catch That Cow【BFS】

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 100475   Accepted: 31438 ...

  5. 初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝

    初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝 令人窒息的创新实验课让我们写程序. POJ-3278 Catch That Cow Farmer John ha ...

  6. POJ3278(BFS)

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

  7. (BFS)Catch That Cow(poj3278)

    题目: 农夫知道一头牛的位置,想要抓住它.农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) .农夫有两种移动方式 ...

  8. 搜索 —— 广度优先搜索(BFS)

    [概述] 广度优先搜索从初始状态 S 开始,利用给定的规则,生成当前状态所有可能的状态,构成的下一层节点,检查是否出现目标状态G,若未出现,就对该层所有状态节点,分别顺序利用规则再次生成再下一层的所有 ...

  9. c语言编辑87152,POJ 3287 (基础BFS) Catch That Cow

    这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE) (1) 把初始节点S0放入Open表中: (2) 如果Open表为空,则问题无解,失败 退出: (3) 把Open表的 ...

最新文章

  1. ​CSRankings年度更新,清华北大包揽AI领域前两名​ | AI日报
  2. python爬虫实例-Python爬虫原理与python爬虫实例大全
  3. 《windows核心编程》 17章 内存映射文件
  4. Linux模块机制浅析
  5. [MATLAB学习笔记]matlab结构体A.B
  6. NAVICAT连接oracle数据库的时候出现ora-01033
  7. Java黑皮书课后题第4章:*4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56“
  8. b类 蚂蚁金服_股权设计与合伙制,解析蚂蚁金服与华为的顶层结构
  9. 一名IT经理是如何把项目带崩的。。。
  10. Linux文件属性4——读取目录文件
  11. iOS关于rar解压第三方库Unrar4iOS使用总结
  12. 公众号支付相关需要注意的问题
  13. 使用tail和head读取字节流
  14. 欧拉项目(python练习)problem 45
  15. 设置Node.js脚本开机自启动
  16. 菜鸟-需求预测与分仓规划
  17. css实现半圆(上下左右半圆)
  18. 《情书》——岩井俊二
  19. 基于tensorflow2.0的深度学习 一
  20. TED演讲:404找不到页面的故事

热门文章

  1. git回滚命令reset、revert的区别
  2. more指令和less指令使用的区别
  3. Python 函数 -next()
  4. leetcode 61 python
  5. java 连接sql_java 连接sql server2008数据库配置
  6. 用Keepalived实现PostgreSQL高可用
  7. 实战演练:如何用BBED恢复删除的数据
  8. 深度解析:Oracle数据库SYSDATE到PostgreSQL数据库最佳转换
  9. Oracle在HPUX IA64平台登陆缓慢问题分析
  10. 一次执行truncate触发ORA-02266解决过程