hrbust 1011 Catch That Cow
来源:互联网 发布:淘宝皇冠和蓝冠 编辑:程序博客网 时间:2024/06/06 01:57
这道题用广搜方便,感觉用深搜的话,很容易在错误方向浪费时间,假如说数据是1,6如果用深搜,那很有可能在1到负数的这个本来就不能的区间内进行搜索,而且还是搜索到底,因为只有搜索到底的时候才能知道会不会到达6,而广搜的话,把在该点的所有情况都放到队列里,然后该点出队,在对入队的几个点判断符不符合,如果不符合就在扩展
#include <iostream>#include <cstring>#define MAX 200001using namespace std;int start,end_,min_;int visit[200001];int que[200001];bool check(int q){ if(q>=0 && q<MAX && visit[q]==-1) return true; else return false;}int bfs(int start_,int endl_){ memset(visit,-1,sizeof(visit)); int head=1,tail=1,now; que[tail]=start_; visit[start_]=0; tail++; while(head<tail) { now=que[head]; head++; if(now==endl_) { min_=visit[endl_]; break; } if(check(now+1)==true) { que[tail]=now+1; visit[now+1]=visit[now]+1; tail++; } if(check(now-1)==true) { que[tail]=now-1; visit[now-1]=visit[now]+1; tail++; } if(check(now*2)==true) { que[tail]=now*2; visit[now*2]=visit[now]+1; tail++; } } return min_;}int main(){ int min; while(cin>>start>>end_) { min=bfs(start,end_); cout<<min<<endl; }}
0 0
- hrbust 1011 Catch That Cow
- HRBUST 1012 Catch That Cow
- HRBUST 1012 Catch That Cow (BFS)
- Hrbust-1012-Catch That Cow(广搜BFS入门)
- POJ3278 Catch That Cow
- Catch That Cow
- Catch That Cow
- poj3278 Catch That Cow
- 3278. Catch That Cow
- 【HDU2717】-Catch that cow
- Catch That Cow
- F - Catch That Cow
- poj3278 - Catch That Cow
- HDOJ Catch That Cow
- 2717Catch That Cow
- POJ3278 Catch That Cow
- 3278Catch That Cow
- Catch That Cow(bfs)
- Tomcat8.5.6源码解析(1)——在Eclipse中构建Tomcat项目
- centos7下快速安装mysql
- ubuntu14.04在vmware10上无法使用unity
- Docker 相关
- 文章标题
- hrbust 1011 Catch That Cow
- 快速幂
- javascript中apply与call的应用与区别
- ssh localhost “Permission denied (publickey)
- CSS简单了解-Web笔记(三)
- spark programming guide
- crack me160题 第二题(序号6) aLoNg3x.1 草稿
- 文章标题
- AndroidManifest.xml文件详解(data)