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 ...
随机推荐
- WINRAR评估版本弹出框消除
网上有很多WINRAR评估版本,这些版本下载安装了之后总会有些广告弹出,让人很烦恼,现在教大家一个方法消除这些弹出框. 复制以下代码: RAR registration data SeVeN U ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
- 在线的JSON formate工具
一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/
- opencv 模板匹配与滑动窗口(单匹配) (多匹配)
1单匹配: 测试图片: code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <open ...
- arcgis下载
你懂的~ t.cn/RA4cc3k 密码ygdr 包含10.2全部,含有(亲测)字样表示测试过OK的,SP是从esri网站下载的几乎全部patch和sp,包括desktop.engine和sever: ...
- Vieta定理
一元$n$次方程$$P(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+\cdots+a_{a}x+a_{0}=a_{n}(x-x_{1})(x-x_{2})\cdots (x-x_{n}) ...
- 蓝桥杯--Quadratic Equation
蓝桥杯--Quadratic Equation 问题描述 求解方程ax2+bx+c=0的根.要求a, b, c由用户输入,并且可以为任意实数. 输入格式:输入只有一行,包括三个系数,之间用空格格开. ...
- Educational Codeforces Round 1 B. Queries on a String 暴力
B. Queries on a String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/59 ...
- Android学习笔记(20)————利用ListView制作带竖线的多彩表格
http://blog.csdn.net/conowen/article/details/7421805 /********************************************** ...
- iOS开发——网络编程Swift篇&(七)NSURLSession详解
NSURLSession详解 // MARK: - /* 使用NSURLSessionDataTask加载数据 */ func sessionLoadData() { //创建NSURL对象 var ...