poj--3278
来源:互联网 发布:重庆时时彩后二软件 编辑:程序博客网 时间:2024/05/18 03:32
基本的bfs
主要是将坐标看出状态,然后就可以可以直接用bfs寻找目标了,
又由bfs的性质可知第一次找到时,时间花费是最小的
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;typedef struct {int x;int time; //记录到达每个节点所需的时间 }D; D d1,d2;int stack[200000];int visited[2000000],k;int bfs(int n){ int i,j; memset(visited,0,sizeof(visited)); queue<D>q; d1.x=n;d1.time=0; q.push(d1); visited[n]=1; while(!q.empty()) { d2=q.front(); q.pop(); int x=d2.x; for(i=0;i<3;i++) { int xx; switch(i) { case 0:xx=x-1;break; case 1:xx=x+1;break; case 2:xx=x*2;break; } if(xx>=0&&xx<=1000000&&!visited[xx]) //注意一下上界的值 { d1.x=xx; d1.time=d2.time+1; if(xx==k) return d1.time; q.push(d1); visited[xx]=1; } } } return 0;}int main(){int n;while(scanf("%d%d",&n,&k)!=EOF){/* if(n==k) { printf("0\n"); continue; }*/ int ans=bfs(n); printf("%d\n",ans);} return 0;}
0 0
- POJ 3278
- POJ 3278
- POJ 3278
- POJ 3278
- poj 3278
- poj 3278
- POJ 3278
- poj 3278
- POJ 3278
- poj 3278
- poj 3278
- poj 3278
- poj-3278
- poj--3278
- poj-3278
- poj 3278
- poj 3278
- poj 3278
- junit及jmockit边学边记
- Linux下安装、启动MySQL
- Fragment(一)快速入门之使用
- js中引用js
- 页面输出Hello World
- poj--3278
- 读写XML
- 国嵌系统编程学习笔记
- 输入分数输出等级
- 数据结构
- SolrCloud 分布式集群安装部署(solr4.8.1 + zookeeper +tomcat)
- 暗示法暗示法暗示法
- 如何学习python
- 撒旦撒旦阿斯达阿斯达