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 ...
随机推荐
- [iOS 多线程 & 网络 - 2.11] - ASI框架上传文件
A.ASI的上传功能基本使用 1.实现步骤 (1)创建请求 使用ASIFormDataRequest (2)设置上传文件路径 (3)发送请求 2.上传相册相片 UIImagePickerCon ...
- ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题
转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...
- JEECMS-自定义标签[list]
1.自定义标签类 import static cn.com.yhxl.common.web.freemarker.DirectiveUtils.OUT_LIST; import static free ...
- Ecshop 学习之路一 2016年6月30日
以前下载ecshop 都是在ecshop官网上下载,前后台模板都很难看.功能也不太齐全,这次在模板堂下载了ecshop 模板 仿小米的.做一个简单的电商网站. 页面结构还是挺简单的.功能也齐全.用ec ...
- 一个简单的hibernate项目
准备工作 工具准备 hibernate3.5.3 Eclipse Java EE IDE for Web Developers.Version: Helios MySQL5.5 环境配置搭建 安装并配 ...
- Java模拟登陆02【转载】
在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...
- Codeforces Round #274 (Div. 1) B. Long Jumps 数学
B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...
- Communications link failure报错的处理
一.报错的问题: 测试环境在做压力测试的时候爆出错误 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications l ...
- tomcat配置虚拟主机
在眼下,非常多server都是一台server对外能够訪问非常多个javaEE的项目,这样的实现方式就须要在tomcat里面配置虚拟主机了!以下就说说怎样配置虚拟主机: 找到tomcat的安装文件夹, ...
- 调用webservice查询手机号码归属地信息
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...