poj 3278 Catch That Cow 优化深搜
这题的思想很简单,就是每次找出队列里面花费时间最少的来走下一步,这样当我们找到k点后,所花费的时间一定是最少的。
但要用一个标记数组vis[200010],用来标记是否走过。否则会内存溢出。
#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int vis[];
struct Point{
int position,Time;
Point(int a,int b)
{
position=a;Time=b;
vis[a]=;
}
int operator <(const Point &temp) const
{
return Time>temp.Time;
}
}; priority_queue<Point> q;
int bfs(int n,int k)
{
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
Point p(n,);
q.push(p);
while(!q.empty())
{
p=q.top();
q.pop();
if(p.position==k)
return p.Time;
if(p.position>k)
{
if(!vis[p.position-])
q.push(Point(p.position-,p.Time+));
}
else
if(p.position>=)
{
if(p.position)
if(!vis[p.position-])
q.push(Point(p.position-,p.Time+));
if(!vis[p.position+])
q.push(Point(p.position+,p.Time+));
if(!vis[p.position*])
q.push(Point(p.position*,p.Time+));
}
}
return ;
}
int main()
{
int n,k,i,j;
while(scanf("%d%d",&n,&k)!=EOF)
{
printf("%d\n",bfs(n,k));
}
return ;
}
poj 3278 Catch That Cow 优化深搜的更多相关文章
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
随机推荐
- HDU 1702 http://acm.hdu.edu.cn/showproblem.php?pid=1702
#include<stdio.h> #include<string.h> #include<queue> #include<stack> #define ...
- java封装对象转json字符串
/** * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). * * Licensed under the Apache License ...
- Simulator模拟器 硬件键盘不能输入
快捷键: Command + Shift +K
- LINUX消息队列实战之一
前言 能说能抄能论皆不算,能写能打才是真功夫. 唠叨 反正我也是一个孤独的程序猿,多说一些奇奇怪怪的唠叨也无妨,第一次写消息队列,书本的东西和实战很不同,根据实战总结的一些注意事项会和大家分享,也敲打 ...
- 终于用scons创建了一个MDK工程
这几天我在学着怎么使用RT-Thread.起初只想用一下里面的RTGUI,却一直没成功,功力实在不行啊. RT-Thread用了scons来创建工程,似乎还能编译,还有很多可配置项,很是方便.于是我想 ...
- 169 Majority Element [LeetCode Java实现]
题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...
- as3中使用stage ,root ,this 区别详解
stage:最顶层舞台root:stage的下一级舞台,属于第二层舞台(继承自DisplayObject)this:当前的对象(如果是主时间轴上的this,那它就是root) 继承方面:Stage - ...
- Computer Science Theory for the Information Age-2: 高维空间中的正方体和Chernoff Bounds
高维空间中的正方体和Chernoff Bounds 本文将介绍高维空间中正方体的一些性质,以及一个非常常见也是非常有用的概率不等式——Chernoff Bounds. 考虑$d$维单位正方体$C=\{ ...
- Android自定义View,高仿QQ音乐歌词滚动控件!
最近在以QQ音乐为样板做一个手机音乐播放器,源码下篇博文放出.今天我想聊的是这个QQ音乐播放器中歌词显示控件的问题,和小伙伴们一起来探讨怎么实现这个歌词滚动的效果.OK,废话不多说,先来看看效果图: ...
- PHP读书笔记(6)- 数组
数组定义 数组就是一个键值对组成的语言结构,键类似于酒店的房间号,值类似于酒店房间里存储的东西.PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型. 定义数组 ...