poj3278
来源:互联网 发布:淘宝卖死人穿过的衣服 编辑:程序博客网 时间:2024/05/29 19:15
Problem : Catch That Cow
Description:Jhon要去抓回他的逃出去的牛,Jhon在任何点X都可以在单位时间内到达X-1或X+1或2X处。假设牛不动,问Jhon最短能用几步抓住他的牛。
Solution:广度优先搜索。
Code(C++):
#include<iostream>#define MAX 1000005using namespace std;struct Point { int value; int step; Point(){} Point(int v,int s=0) { value=v; step=s; }};int visit[MAX];Point queue[MAX];int Bfs(int x,int y){ if(x==y) return 0; int head=0,tail=0,i; memset(visit,0,sizeof(visit)); Point start(x,0); queue[tail]=start; tail++; visit[x]=1; while(head!=tail) { Point temp=queue[head]; head++; head%=MAX; Point n[3]={ Point(temp.value-1,temp.step+1), Point(temp.value+1,temp.step+1), Point(temp.value*2,temp.step+1) }; for(i=0;i<3;i++) { if(n[i].value==y) return n[i].step; if(n[i].value>=0&&n[i].value<1000005&&!visit[n[i].value]) { queue[tail]=n[i]; tail++; tail%=MAX; visit[n[i].value]=1; } } }}int main(){ int m,n; cin>>m>>n; cout<<Bfs(m,n)<<endl; return 0;}
0 0
- POJ3278
- POJ3278
- POJ3278
- poj3278
- poj3278
- poj3278
- POJ3278
- POJ3278
- poj3278
- poj3278
- poj3278
- POJ3278
- poj3278
- poj3278
- poj3278
- poj3278
- POJ3278
- POJ3278
- js 模块编程 module.exports require
- BroadcastReceiver
- Dubbo开发调试方法
- css伪类选择器及其兼容性
- ajax如何处理服务器返回的三种数据类型
- poj3278
- 小谈网易云音乐----API
- linux中bin和sbin目录的主要区别
- 服务器推送之WebSocket 入门学习路程(三)
- 【爬虫-反爬虫】系列一:反爬虫之session(4)
- SDKD 2016 Summer Single Contest #03.A
- ScrollView中EditText导致自动滚动问题
- Python常用的第三方模块——学习笔记
- hibernate学习——HibernateUtil