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 ...
随机推荐
- nginx 502 错误
今天帮朋友处理一个程序报错,重启nginx服务之后,发现首页打不开了,但是静态文件可以打开 经检查nginx 服务器正常运行,重启无数次仍然502错误,考虑到静态文件可以打开,怀疑可能是php 脚本程 ...
- 关闭SELinux的两种方法
1 永久方法 – 需要重启服务器 修改/etc/selinux/config文件中设置SELINUX=disabled ,然后重启服务器. 2 临时方法 – 设置系统参数 使用命令setenforce ...
- linux下登陆用户的行为信息—w和who命令详解
查看用户的操作系统管理员若想知道某一时刻用户的行为,只需要输入命令w 即可,在SHELL终端中输入如下命令: [root@localhost ~]# w 可以看到执行w命令及显示结果. 命令信息含义上 ...
- Light oj 1236 - Pairs Forming LCM (约数的状压思想)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...
- Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关
资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载. 注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBu ...
- [oracle]一个最简单的oracle存储过程"proc_helloworld"
1.编写.编写一个最最简单的存储过程,给它起个名字叫做proc_helloworldCREATE OR REPLACE PROCEDURE proc_helloworldISBEGIN DBMS_ ...
- HibernateDaoSupport和HibernateTemplate
hibernateTemplate的常用方法: Ø void delete(Object entity):删除指定持久化实例 Ø deleteAll(Collection entities):删除 ...
- string <-> wstring
// std::string -> std::wstringstd::string s("string");std::wstring ws;ws.assign(s.begin ...
- HDU 1080 DP
匹配两个人相似度. A,G,C.T.每两个都会有一个相应的值,给出两串基因.长度能够不一样,能够在基因中间加_使两串长度一样.然后有一个相应值.求最大相应值. 先做出相应的表 DP方程: x=dp[i ...
- EasyBCD 2.2中文版安装变色龙wowpc.iso详细教程(适用各个版本)
第一章 安装变色龙引导本章节提供3种安装方案,请自行选择 1.使用 Windows 版变色龙安装器安装适用引导方案:BIOS+MBR第1步:下载 Chameleon Install 2.2svn228 ...