poj3278 Catch That Cow(bfs搜索)
2013-08-27 20:13
363 查看
题目大意:一个农夫追赶一只逃跑的牛,农夫与牛在一条直线上,农夫位置为n,牛位置为m,农夫的位置可以每秒变换为n-1,n+1或2n,牛的位置不变,问最短什么时候农夫可以追到牛。
分析:就是一个简单的爆搜题,直接bfs即可,记得注意一下细节。
代码:
分析:就是一个简单的爆搜题,直接bfs即可,记得注意一下细节。
代码:
#include<iostream> #include<stdio.h> #include<cstring> using namespace std; int n,m; int vis[100001];//vis记录是否搜过 int que[100001],num[100001];//que队列,记录已搜到的数,num记录从起始位置到当前位置最短时间 int get_v(int u,int i) { if(i!=0) return (i==1?u+1:u*2); return u-1; } int dfs() { int rear=1,front=0; int temp,next; memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); vis =1; que[front]=n; while(rear>front) { temp=front++; if(que[temp]==m) return num[m];//注意这里,返回值 for(int i=0;i<3;i++) { next=get_v(que[temp],i); if(next>=0&&next<=100000&&!vis[next])//条件不需太大 { vis[next]=1; que[rear++]=next; num[next]=num[que[temp]]+1; } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n>m) printf("%d\n",n-m); else printf("%d\n",dfs()); } return 0; }
相关文章推荐
- 广度优先搜索bfs与抓住那头奶牛(Catch that cow, poj3278)
- 【搜索BFS】poj3278--Catch That Cow(bfs)
- poj3278 Catch That Cow BFS搜索 TWT Tokyo Olympic 2combo-1
- poj3278——Catch That Cow(BFS,剪枝)
- POJ3278 Catch That Cow BFS入门
- poj 3278 Catch That Cow (bfs搜索)
- poj3278 Catch that cow 代码比很多人精简。。。BFS
- poj3278 Catch That Cow bfs
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
- POJ 3278-Catch That Cow 广度优先搜索BFS
- hdu 2717 Catch That Cow bfs搜索 解题报告
- [kuangbin带你飞]专题一 简单搜索 Catch That Cow : bfs
- POJ3278 - Catch That Cow - 广度优先搜索
- 【poj3278】Catch that cow——bfs
- POJ3278 HDU2717 Catch That Cow【BFS】
- POJ3278 Catch That Cow(BFS入门)
- POJ3278 Catch That Cow(BFS)
- Poj3278 Catch That Cow ( BFS
- poj 3278 Catch That Cow (bfs搜索)
- poj3278--Catch That Cow(BFS+裁剪记录)