poj 3278 bfs 标记数组
来源:互联网 发布:数控车床电脑自动编程 编辑:程序博客网 时间:2024/06/05 11:07
第一道bfs的题,哈哈,貌似大家都是
可知移动一次可以有三种操作选择,就把它加到队列里头,每循环一次,取出队头并出队,
创建一个标记数组用来记录这个点有没有访问到,因为第一次访问到的地方所用的步数是最小的,即使之后用其他的操作同样可以走到,同时用它来记录步数,因为0表示没有访问,非0表示已经访问过
如果n>=k 输出n-k 因为只能往回走
注意 数组要开大点,因为当k=100000时 假设其中某一步通过*2走到了100099,往回走要比在上一状态单纯+1所用的步数少
#include<map>#include<set>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int vis[100104];int main(){ int n,k; while(scanf("%d%d",&n,&k)!=EOF) { if(n>=k) { printf("%d\n",n-k); } else { queue<int> q; q.push(n); int step=0; int cnt=0; memset(vis,0,sizeof(vis)); while(!q.empty()) { cnt++; step=q.front(); q.pop(); int step1; if(step+1<=100100&&!vis[step+1]) { vis[step+1]=vis[step]+1; if(step+1==k) break; q.push(step+1); } if(step-1>=0&&!vis[step-1]) { vis[step-1]=vis[step]+1; if(step-1==k) break; q.push(step-1); } if(step*2<100100&&!vis[step*2]) { vis[step*2]=vis[step]+1; if(step*2==k) break; q.push(step*2); } } printf("%d\n",vis[k]); } } return 0;}
0 0
- poj 3278 bfs 标记数组
- HDOJ2579,BFS(三维数组标记)
- POJ-3278(bfs定界和标记)(Catch That Cow)
- poj 1742 多重背包、数组标记法
- poj-2250Compromise(LCS+标记数组)
- hdoj 2579 Dating with girls(2) 【BFS&&三维数组标记】
- HDU 2364 Escape (多维数组标记状态bfs)
- uva 10047 The Monocycle (bfs||思维数组标记状态)
- POJ 3278 : 经典BFS
- poj 3278 简单bfs
- poj 3278 bfs
- poj 3278、、、BFS
- poj 3278 bfs
- poj-3278-bfs
- poj 3278 bfs
- POJ 3278 BFS
- poj 3278 BFS
- POJ 3278 简单 BFS
- 选择排序和插入排序
- POJ 1734 Sightseeing trip【floyd求最小环+记录路径】
- Android 自定义view设置xml属性
- 虚拟机下破解CentOS root密码
- springmvc集成JSR-303的解析消息文件的默认实现浅析
- poj 3278 bfs 标记数组
- RxJava开发精要2-为什么是Observables?
- Android控件之RecyclerView的基本使用
- 【Github教程】史上最全github使用方法:github入门到精通
- python下根据csv将数据集按照类别分文件夹放置
- 关于expdp在服务器上同时只能有一个进程在执行的问题
- [易飞]事务进程被另一进程锁死 解决办法
- Django学习笔记--后台管理界面
- 创建容器Error response from daemon: dial tcp 72.52.4.91:8080: connection refused