POJ 3278 Catch That Cow <BFS>
来源:互联网 发布:北京骏嘉财通弘历软件 编辑:程序博客网 时间:2024/06/03 22:39
题目:传送门
分析:超简单的bfs,稍微剪枝一下就好了,比如X-1要判断>=0,X+1要判断<=k,X*2要判断小于K*2。最后注意的就是N==K这种情况。
代码:
#include <iostream>#include <algorithm>#include <queue>#include <cstring>using namespace std;const int MAXN=2e5+10;int n,k;struct Node{ int x,time; Node(int _x,int _time):x(_x),time(_time){}};queue<Node> q;bool vis[MAXN];void bfs(){ if(n==k){ cout<<0<<endl; return ; } while(!q.empty()) q.pop(); memset(vis,false,sizeof vis); q.push(Node(n,0)); vis[n]=true; while(!q.empty()){ Node cur=q.front(); q.pop(); int xx=cur.x+1; if(xx==k){ cout<<cur.time+1<<endl; return ; } if(xx<k&&!vis[xx]){ vis[xx]=true; q.push(Node(xx,cur.time+1)); } xx=cur.x-1; if(xx==k){ cout<<cur.time+1<<endl; return ; } if(xx>=0&&!vis[xx]){ vis[xx]=true; q.push(Node(xx,cur.time+1)); } xx=cur.x*2; if(xx==k){ cout<<cur.time+1<<endl; return ; } if(xx<k*2&&!vis[xx]){ vis[xx]=true; q.push(Node(xx,cur.time+1)); } }}int main(){ cin>>n>>k; bfs(); return 0;}
阅读全文
0 0
- poj 3278 Catch That Cow BFS
- poj 3278 Catch That Cow BFS
- POJ 3278 Catch That Cow BFS
- poj 3278 bfs(Catch That Cow)
- poj 3278 Catch That Cow(bfs)
- POJ 3278 Catch That Cow bfs
- POJ 3278 Catch That Cow 【bfs+队列】
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow(BFS)
- poj 3278 Catch That Cow bfs
- poj 3278 Catch That Cow (BFS剪枝)
- POJ 3278 Catch That Cow[BFS]
- Poj 3278 Catch That Cow 裸bfs
- POJ 3278 Catch That Cow(图论:BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow (BFS)
- poj 3278 Catch That Cow 【BFS】
- POJ--3278:Catch That Cow (BFS)
- 视频直播知识点(二)
- sql简单实现查询某个表字段名,是否主键,是否非空及备注信息
- QT中 mdisubwindow中让图片跟随窗口变化
- Android ActivityManagerService根据oom_adj数值内存回收机制
- RTSP建立及消息处理(live555)
- POJ 3278 Catch That Cow <BFS>
- Eclipse上配置Maven插件
- 决策树(ID3算法)Python实现
- 字符串联练习
- putty实现从windows向linux传送文件
- jquery实现table行复制粘贴功能
- A
- SSH KEY 配置
- android库的冲突