hdu2717 简单广搜
来源:互联网 发布:网络配电箱 编辑:程序博客网 时间:2024/05/07 16:01
step[],记录到目标位置的层数;
#include<cstdio>#include<iostream>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<cmath>#include<cstring>#include<queue>using namespace std;int N,K;int ans;int visit[200010];int step[200010];void bfs(){ queue<int> q; q.push(N); int tmp; visit[N]=1; while(!q.empty()){ tmp=q.front(); if(tmp==K) return; q.pop(); if(tmp+1<=100000&&!visit[tmp+1])//没有此限制条件就会超内存 { visit[tmp+1]=1; q.push(tmp+1); step[tmp+1]=step[tmp]+1; } if(tmp-1>=0&&!visit[tmp-1])//如果将tmp-1>=0改为tmp-1<=1000000则会runtime error { visit[tmp-1]=1; q.push(tmp-1); step[tmp-1]=step[tmp]+1; } if(tmp*2<=200000&&!visit[2*tmp]){ visit[2*tmp]=1; q.push(2*tmp); step[tmp*2]=step[tmp]+1; }}}int main(){ while(cin>>N>>K){ for(int i=0;i<200010;i++) step[i]=visit[i]=0; bfs(); cout<<step[K]<<endl; } return 0;}
- hdu2717 简单广搜
- hdu2717(裸bfs广搜)
- HDU2717 Catch That Cow 【广搜】
- POJ1376简单广搜
- 广搜简单题
- 简单广搜:Nightmare
- hdu2717
- hdu2717
- hdu2717
- hdu2717
- hdu2612 简单两次广搜
- POJ 3278 简单广搜
- POJ1426(简单广搜)
- POJ 3278(简单广搜)
- 简单广搜:Knight Moves
- Dice (简单广搜)
- hdu2717 Catch That Cow BFS简单题
- 深搜和广搜简单对比
- java常见错误汇总——string data, right truncation
- Tddl分析之一
- Nhibernate 不要降级混用visual studio的程序集
- ASP.NET 应用程序 的神奇错误一
- 关于 /usr 和 /usr/local 的讨论
- hdu2717 简单广搜
- Android实现自定义分享功能
- iOS如何设置app支持文件共享
- C#操作xml
- ucOS的时钟管理
- Python Challenge 05 Solution
- 第11课 日历应用程序
- 查询SQL Server 数据表中不包含某一列的SQL语句该怎么写?
- Sparse Autoencoder1-NeuralNetworks