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 ...
随机推荐
- luogu P2303 [SDOi2012]Longge的问题
传送门 \[\sum_{i=1}^{n}\gcd(i,n)\] 考虑枚举所有可能的gcd,可以发现这一定是\(n\)的约数,当\(\gcd(i,n)=x\)时,\(gcd(\frac{i}{x},\f ...
- NOI2018场外游记
鬼晓得APIO以后我经历了些什么 Day 0 好像没什么要记的 Day 1 下午去参加开幕式 神tm大型落地柜装风扇空调下放冰块 开幕式,,,hot chocolate是真的hot(强制在线?卡常?) ...
- [SDOI2010]古代猪文 (欧拉,卢卡斯,中国剩余)
[SDOI2010]古代猪文 \(solution:\) 这道题感觉综合性极强,用到了许多数论中的知识: 质因子,约数,组合数 欧拉定理 卢卡斯定理 中国剩余定理 首先我们读题,发现题目需要我们枚举k ...
- vue学习一:新建或打开vue项目(vue-cli2)
vue-cli3的操作参考文章:vue/cli 3.0脚手架搭建,浅谈vue-cli 3 和 vue-cli 2的区别 1.前期准备: node.js环境,安装node npm或者cnpm(npm的淘 ...
- 超详细的Web前端开发规范文档
规范目的为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进行前台页面开发. 本文档如有不对或者不合 ...
- SpringBoot整合国际化功能
(1).编写国际化配置文件 在resources下新建i18n文件夹,并新建以下文件 ①index.properties username=username ②index_en_US.proper ...
- 使用matplotlib绘制多个图形单独显示
使用matplotlib绘制多个图形单独显示 一 代码 import numpy as np import matplotlib.pyplot as plt #创建自变量数组 x= np.linspa ...
- bzoj2588 Spoj10628. count on a tree
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- apache httpd.conf
Apache的主配置文件:/etc/httpd/conf/httpd.conf 默认站点主目录:/var/www/html/ Apache服务器的配置信息全部存储在主配置文件/etc/httpd/co ...
- CentOS 6.5 rsync+inotify实现数据实时同步备份
CentOS 6.5 rsync+inotify实现数据实时同步备份 rsync remote sync 远程同步,同步是把数据从缓冲区同步到磁盘上去的.数据在内存缓存区完成之后还没有写入到磁盘 ...