POJ.3278 Catch That Cow (BFS)
来源:互联网 发布:python split最后一个 编辑:程序博客网 时间:2024/06/04 18:55
POJ.3278 Catch That Cow (BFS)
题意分析
求最少的操作次数,暴力就用BFS。
这题坑点挺多,一开始交上去无限RE,摸不着头脑,后来发现后数组越界了。因为存在-1的操作和*2的操作 ,不加判断就直接越界。
这告诉我们一个道理,光把数组开大点,是没有用的。
代码总览
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <sstream>#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#define INF 0x3f3f3f3f#define nmax 400010#define MEM(x) memset(x,0,sizeof(x))using namespace std;int num, tar;int ans;struct s{ int num; int time;};bool visit[nmax];void bfs(int num,int time){ queue<s> q; s now = {num,time},temp; visit[num] = true; q.push(now); while(!q.empty()){ temp = q.front();q.pop(); if(temp.num == tar){ ans = temp.time; break; } if(!visit[temp.num+1]){ now.num = temp.num+1; now.time = temp.time+1; visit[temp.num+1] = true; q.push(now); } if(temp.num-1>=0 && !visit[temp.num-1] ){ now.num = temp.num-1; now.time = temp.time+1; visit[temp.num-1] = true; q.push(now); } if(temp.num*2<nmax && !visit[temp.num*2] &&temp.num*2 >=0 ){ now.num = temp.num*2; now.time = temp.time+1; visit[temp.num*2] = true; q.push(now); } }}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d %d",&num,&tar)!=EOF){ MEM(visit); ans = INF; bfs(num,0); printf("%d\n",ans); } return 0;}
0 0
- poj 3278 Catch That Cow BFS
- poj 3278 Catch That Cow BFS
- POJ 3278 Catch That Cow BFS
- poj 3278 bfs(Catch That Cow)
- poj 3278 Catch That Cow(bfs)
- POJ 3278 Catch That Cow bfs
- POJ 3278 Catch That Cow 【bfs+队列】
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow(BFS)
- poj 3278 Catch That Cow bfs
- poj 3278 Catch That Cow (BFS剪枝)
- POJ 3278 Catch That Cow[BFS]
- Poj 3278 Catch That Cow 裸bfs
- POJ 3278 Catch That Cow(图论:BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow (BFS)
- poj 3278 Catch That Cow 【BFS】
- POJ--3278:Catch That Cow (BFS)
- JAVA源码解析-LinkedList源码
- 每日三题-Day5-C(HDU 1078 FatMouse and Cheese 记忆化搜索)
- hdu 4770 Lights Against Dudely(暴力枚举dfs)
- 基础算法(二)---数据结构之树
- 创意反馈与授权——使用技巧
- POJ.3278 Catch That Cow (BFS)
- 中国创业者应该跟硅谷学习什么
- 单个舵机串口控制
- 核心驱动力——所有权与拥有感
- luogu P1064 金明的预算方案
- 社交影响与关联性——使用技巧
- UIView设置圆角、边框和阴影
- Cocoa框架-Core Location
- 你不知道的颠覆式创新者