bfs-poj3278
题目链接:http://poj.org/problem?id=3278


题意:农夫原始在N位置上,他的目的是要抓到在K位置上的牛。牛的位置是固定不变的,而农夫的移动是在一条水平线上进行的,移动方式有以下三种1、从当前位置往后移动一格;2、从当前位置往前移动一格;3、从当前位置pos瞬移到2*pos位置上。
刚开始我想利用DP来解决这道题,但是以失败告终,所以便参考了别人的解法,发现大多数人都是使用bfs来解决的。于是我改用bfs来处理。
代码实现:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=;//此处范围得准确,如果小了会Wrong int vis[maxn],step[maxn];
queue<int>que; int bfs(int N,int K)
{
int next,top;
que.push(N);//农夫所在初始点入队
vis[N]=true;//标记已被访问
step[N]=;//步数初始为0
while(!que.empty()){//队列非空时,执行循环
top=que.front();//取出队首
que.pop();//弹出队首
for(int i=;i<;i++){
if(i==) next=top-;
else if(i==) next=top+;
else next=top*;
if(next<||next>=maxn) continue;//如已出界,则排除该情况
if(!vis[next]){//如果改点还未被访问过
que.push(next);//入队
step[next]=step[top]+;//步数+1
vis[next]=true;//标记已被访问过
}
if(next==K) return step[next];//当遍历到结果,返回步数
}
}
}
int main()
{
int N,K;
while(~scanf("%d%d",&N,&K)){
memset(step,,sizeof(step));
memset(vis,false,sizeof(vis));
while(!que.empty()) que.pop();//注意调用前要先清空
if(N>=K) printf("%d\n",N-K);
else printf("%d\n",bfs(N,K));
}
return ;
}
bfs-poj3278的更多相关文章
- 超超超简单的bfs——POJ-3278
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 89836 Accepted: 28175 ...
- 空间最短路径,BFS(POJ3278)
题目链接:http://poj.org/problem?id=3278 #include <cstdio> #include <queue> #include <stri ...
- poj分类解题报告索引
图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
- 北大ACM - POJ试题分类
1.入门水题 可用于练手与增强自信 POJ-1003POJ-1004 POJ-1005 POJ-1207 POJ-3299 POJ-2159 POJ-1083POJ-3094 2.初级 2.1. 基本 ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- POJ-3278(BFS)
题目: ...
- poj3278 BFS入门
M - 搜索 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit I ...
- POJ3278 Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj3278 【BFS】
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 97240 Accepted: 30519 ...
随机推荐
- (原创 开源)AppWidge的使用—桌面便利贴
Android平台的一大特色就是支持桌面插件——AppWidget. 且不说,AppWidget是否会影响系统的流畅性,AppWidget确实是满足了用户个性化和快捷操作的需要. 常见的AppWidg ...
- jquery选择器最后一个,倒数第二个元素
<div> <p>1</p> <p>2</p> <p>3</p> <p>4</p> < ...
- ubuntu14.04 放开串口权限
可以用如下命令查看串口信息: ls -l /dev/ttyUSB*来查看相关的信息. 但是普通用户没有usb操作权限(函数open()打不开串口:refused),如果我们想在ROS程序里面打开串口, ...
- ubuntu + usb转RS232驱动
1. 购买USB转串RS232/485/422 如果你的电脑有串口的话,就不用买啦,我的台式机有串口,把USB转串的线插上之后,unbuntu就不支持了.(自己有嘛) 就是输入 ls /dev/tt ...
- 【windows核心编程】注入DLL时BUG排除与调试
DLL注入排除bug的思路步骤. 1.在VS中监视输入err,hr检查DLL是否注入成功 2.OD断点loadlibraryW,loadlibraryA是否已经注入成功,eax是否有值. 3.检查路径 ...
- 【vim】保存文件并退出 :w=:wq
这两个命令实际上并不完全等价,当文件被修改时两个命令时相同的.但如果未被修改,使用 :x 不会更改文件的修改时间,而使用 :wq 会改变文件的修改时间.
- k64 datasheet学习笔记4---Memory Map
1.前言 本文主要介绍K64地址空间的映射 2. System Memory Map 3. K64地址映射 4. Armv7m地址映射 4.1 Armv7M.System地址段(0XE0000000~ ...
- kindle转换工具-calibre
kindle转换工具 calibre https://calibre-ebook.com/download_windows
- 说说流控制(RTS/CTS/DTR/DSR 你都明白了吗?)【转】
转自:http://bbs.ednchina.com/BLOG_ARTICLE_129041.HTM 以前写的博文,转过来 ============== 先引用一篇网文,作者不详,因几个地方都说自己是 ...
- 如何在linux下检测内存泄漏(转)
本文转自:http://www.ibm.com/developerworks/cn/linux/l-mleak/ 本文针对 linux 下的 C++ 程序的内存泄漏的检测方法及其实现进行探讨.其中包括 ...