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 ...
随机推荐
- C#中富文本编辑器Simditor带图片上传的全部过程(MVC架构的项目)
描述:最近c#项目中使用富文本编辑器Simditor,记录一下以便以后查看. 注:此项目是MVC架构的. 1.引用文件 项目中引用相应的css和js文件,注意顺序不能打乱,否则富文本编辑器不会正常显示 ...
- extern的作用
#include <stdio.h>extern int a;static int a;extern int b;int b;static int c;extern int c;
- A - The Water Bowls POJ - 3185 (bfs||高斯消元)
题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...
- Bootstrap模态框(一个页面显示多个)的使用
在一个页面显示多个模态框时要讲每个模态框用div包裹起来,否咋会产生格式错误. <html> <head> <meta charset="utf-8" ...
- 【转】Linux中包管理与定时任务
[转]Linux中包管理与定时任务 第1章 软件查询 1.1 查询软件是否安装 rpm -qa |grep cron 查询是否安装了这个软件. [root@znix ~]# rpm -qa |grep ...
- emmc基础技术8:操作模式2-device identification mode
1.前言 eMMC总线操作包含: boot mode, device identification mode interrupt mode data transfer mode 本文主要描述devic ...
- git入门与实践【转】
转自:http://www.cnblogs.com/shenhaocn/archive/2011/03/13/1982957.html 什么是版本控制 要了解什么是git,首先需要了解什么是版本控制( ...
- python的技巧和方法你了解多少?
学了这些你的python代码将会改善与你的技巧将会提高. 1. 路径操作 比起os模块的path方法,python3标准库的pathlib模块的Path处理起路径更加的容易. 获取当前文件路径 前提导 ...
- scrapy通过修改配置文件发送状态邮件
EXTENSIONS = { 'scrapy.extensions.statsmailer.StatsMailer': 500,} STATSMAILER_RCPTS = ['159882826 ...
- sql 学习
.查看表结构用desc desc emp; 2.空表dual,最常用的空表,如: select 2 * 4 from dual; select sysdate from dual; 3.双引号 ...