POJ3278 Catch That Cow(BFS)
来源:互联网 发布:php二维数组查找键值 编辑:程序博客网 时间:2024/05/01 04:45
题目点我点我点我
题目大意:输入两个数n,k两个数,问从n到k最少经过多少步,对于一个数x有三种走法,x-1,x+2,2*x。
思路:简单BFS,需要注意的是,先进行判断x-1,x+2,2*x是否有超出范围,再判断标记数组vis的值,不然会RE到你哭……不懂原因,读者若知道请务必留言告诉我,拜谢……
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define inf 0x3f3f3f3fconst int maxn=200300;int vis[maxn],N,K;int bfs(){ vis[N]=0; queue<int>q; q.push(N); while(!q.empty()) { int t=q.front(); q.pop(); if(t==K)return vis[K]; if(2*t<maxn && vis[2*t]==-1 ) { vis[2*t]=vis[t]+1; q.push(t*2); } if(t-1>=0 && vis[t-1]==-1 ) { vis[t-1]=vis[t]+1; q.push(t-1); } if( t+1<maxn && vis[t+1]==-1) { vis[t+1]=vis[t]+1; q.push(t+1); } }}int main(){ while(~scanf("%d%d",&N,&K)) { memset(vis,-1,sizeof(vis)); printf("%d\n",bfs()); } return 0;}
0 0
- poj3278 Catch That Cow(bfs搜索)
- POJ3278 Catch That Cow(BFS)
- POJ3278 Catch That Cow(bfs)
- poj3278 Catch That Cow(BFS)
- POJ3278:Catch That Cow(BFS)
- POJ3278,Catch That Cow,BFS...
- poj3278 Catch That Cow---bfs
- poj3278 Catch That Cow bfs
- POJ3278 Catch That Cow(BFS)
- poj3278 Catch That Cow(BFS)
- POJ3278 Catch That Cow BFS
- [bfs] poj3278 Catch that Cow
- POJ3278 Catch That Cow(BFS)
- poj3278 Catch That Cow BFS
- POJ3278 Catch That Cow 【BFS】
- poj3278 Catch That Cow BFS
- Poj3278 Catch That Cow ( BFS
- Poj3278 BFS Catch That Cow
- apache显示文件列表
- 题目1035:找出直系亲属
- 工作心得(二)——如何将Nexus本地仓库和Maven结合起来
- C语言基本内容(二)
- 10. Regular Expression Matching
- POJ3278 Catch That Cow(BFS)
- 决策树系列之三 GBDT(MART) 迭代决策树入门教程 | 简介
- ThinkPHP_MVC
- OGG文件的数据结构以及读取其注释信息的代码
- django开发基本网页和使用MySQL
- HTML中的<meta/>标签
- POJ 3020 - Antenna Placement
- linux下的中断(interrupt)IRQ以及IRQ绑核小结
- tkinter学习笔记之messagebox