POJ 3278 Catch That Cow(kuangbin带你飞 专题一:简单搜索)
来源:互联网 发布:淘宝官网下载免费下载 编辑:程序博客网 时间:2024/05/22 17:32
数字的简单BFS,给定起点终点和方式,可以对这个数字进行+1,-1,*2,因为k的范围是0-100000,所以数组起码开20W,小于0的情况直接不考虑。注意越界问题...
#include<cstdio>#include<cstring>#include<stdlib.h>#include<algorithm>#include<queue>#include<cmath>using namespace std;int used[300020];int n,k;struct node{ int num,t;};int ok(int x){ if(x<0||x>200000)//越界 return 0; if(used[x]==1) return 0; return 1;}int bfs(){ queue<node> q; node now,next; now.num=n,now.t=0; q.push(now); memset(used,0,sizeof(used)); used[now.num]=1; while(!q.empty()) { now=q.front(); q.pop(); if(now.num==k) return now.t; next.num=now.num-1; next.t=now.t+1; if(ok(next.num)) { q.push(next); used[next.num]=1; } next.num=now.num+1; next.t=now.t+1; if(ok(next.num)) { q.push(next); used[next.num]=1; } next.num=now.num*2; next.t=now.t+1; if(ok(next.num)) { q.push(next); used[next.num]=1; } } return -1;}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { int ans=bfs(); printf("%d\n",ans); } return 0;}
0 0
- POJ 3278 Catch That Cow(kuangbin带你飞 专题一:简单搜索)
- [kuangbin带你飞]专题一 简单搜索C - Catch That Cow(POJ 3278)
- [kuangbin带你飞]专题一 简单搜索 C - Catch That Cow poj 3278
- poj 3728 Catch That Cow ([kuangbin带你飞]专题一 简单搜索)
- [kuangbin带你飞]专题一 简单搜索 -C - Catch That Cow
- [kuangbin带你飞]专题一 简单搜索 Catch That Cow : bfs
- 3278 & [kuangbin带你飞]专题一 简单搜索 C
- kuangbin带你飞 专题一 简单搜索 (题解)
- POJ 2251 Dungeon Master (kuangbin带你飞 专题一:简单搜索)
- POJ 3984 迷宫问题(kuangbin带你飞 专题一:简单搜索)
- POJ 1426 Find The Multiple(kuangbin带你飞 专题一:简单搜索)
- [kuangbin带你飞]专题一 简单搜索 B - Dungeon Master(POJ 2251)
- POJ 1426 Find The Multiple([kuangbin带你飞]专题一 简单搜索 )
- poj 3126 Prime Path([kuangbin带你飞]专题一 简单搜索)
- POJ 3414 Pots([kuangbin带你飞]专题一 简单搜索)
- POJ 3087 Shuffle'm Up(kuangbin带你飞 专题一:简单搜索)专题一完结
- [kuangbin神带你飞]专题一 简单搜索
- Fire Game [kuangbin带你飞]专题一 简单搜索
- Spring3 MVC + jQuery easyUI 做的ajax版本用户管理
- 数学在计算机图形学中的应用
- 【Android界面实现】关于Android中9patch图的一点小解释
- 软件项目经理必备素质
- Lua与json字符串转换之UTF8
- POJ 3278 Catch That Cow(kuangbin带你飞 专题一:简单搜索)
- UIImage中两种获取图片的方法的区别
- hdu---1425 sort
- aip.jdk1.4 1.5(5.0) 1.6(6.0) 7.0 8.0特点比较与不同
- Spring加载方式ContextLoaderListener和ContextLoaderPlugIn
- A beautiful story about NYC weather
- Xcode6添加.pch,xcode6.pch
- 21.typedef
- 【Android开发经验】ViewHolder到底用什么修饰?static?final?static final?