bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 bfs
来源:互联网 发布:1024控台控制帕灯编程 编辑:程序博客网 时间:2024/06/01 09:05
题意
有一个数轴,现在给出起点n和终点k,每次可以走到n+1或n-1或2*n,求最少走多少步可以走到k。
0<=n,k<=100000
分析
一开始还以为是玄学dp……
其实就是个广搜,上界为max(n,k*2),然后用一个数组f[i]表示走到点i最少需要多少步。
代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<queue>using namespace std;int f[400005],n,m;queue <int> q;void bfs(){ q.push(n); f[n]=1; while (!q.empty()) { int u=q.front(); q.pop(); if (u+1<=max(n,m*2)&&!f[u+1]) { f[u+1]=f[u]+1; if (u+1==m) return; q.push(u+1); } if (u-1>=0&&!f[u-1]) { f[u-1]=f[u]+1; if (u-1==m) return; q.push(u-1); } if (u*2<=max(n,m*2)&&!f[u*2]) { f[u*2]=f[u]+1; if (u*2==m) return; q.push(u*2); } }}int main(){ scanf("%d%d",&n,&m); bfs(); printf("%d",f[m]-1); return 0;}
0 0
- bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 bfs
- BZOJ 1646[Usaco2007 Open]Catch That Cow 抓住那只牛 双向bfs版
- poj 3278 && bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(BFS)
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
- 【BZOJ 1646】 [Usaco2007 Open]Catch That Cow 抓住那只牛
- BZOJ 1646:[Usaco2007 Open]Catch That Cow 抓住那只牛
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
- bzoj'1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
- bzoj1646 [Usaco2007 Open]Catch That Cow 抓住那只牛
- [BZOJ1646] [Usaco2007 Open]Catch That Cow 抓住那只牛
- BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
- 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛
- POJ 3278:Catch That Cow 抓住那头牛
- 广度优先搜索bfs与抓住那头奶牛(Catch that cow, poj3278)
- C++广度优先搜索算法之抓住那头牛(Catch that cow)
- OpenJudge 2.5-2971 Catch That Cow(抓住那头牛)
- Catch That Cow(bfs)
- Catch That Cow+BFS
- 摄像头对焦Vuforia摄像机采集内容模糊
- Android Studio导出Jar包
- 链表操作实例(创建节点,增加节点,倒序链表,销毁链表等)
- 银行取款程序 线程
- MIT6.824 Lab 3: Fault-tolerant Key/Value Service (1)
- bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 bfs
- 安装ubuntu16.04遇到错误-->无法将grub-efi-amd64-signed软件包安装到/target/中
- 4-10 二分查找
- BZOJ 1578 [Usaco2009 Feb]Stock Market 股票市场
- HDU2209+POJ3279 枚举+dfs
- java singleton
- nginx+tomcat+redis 负载均衡和session共享
- 【实战-Linux】--搭建CA认证中心实现https取证
- Tomcat怎么安装SSL证书方法教程(自主csr版)进阶篇(一)