POJ 3278 Catch That Cow(BFS 剪枝)
题目链接:http://poj.org/problem?id=3278
这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天。 = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来。0 0
先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置。
并且农夫有三种移动方式,X + 1,X - 1,X * 2。问最少几步抓到牛。
開始觉得非常easy的,三方向的BFS就能顺利解决。然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前。牛早跑了。
然后反应过来开标记。来节约时间,然后发现竟然RE了,预计是标记数组不够大,毕竟有一个方向的x * 2。
然后打算控制一下查找范围。由于当X到达某个范围之后,在变为K,肯定不会是最少路径。
比方。当X已经大于K了。再运行X + 1, X * 2,肯定不会最短的得到K,然后在推断标记的时候做了优化。
然后就过掉了。
我再去百度。看看有没有更好的方法,发现我这种方法就叫剪枝啊 = =。吓尿、
代码例如以下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> #define LEN 1000000
struct node
{
int num,t;
}q[LEN]; bool vis[10000000]; int bfs (int x,int k)
{
int s = 0,e = 0; q[s].num = x;
q[s++].t = 0;
vis[x] = 1;
while (s != e)
{
struct node tmp = q[e++];
e %= LEN; if (tmp.num == k)
return tmp.t; if (!vis[tmp.num + 1] && tmp.num <= k)
{
q[s].num = tmp.num + 1;
q[s++].t = tmp.t +1;
s %= LEN;
vis[tmp.num + 1] = 1;
} if (!vis[tmp.num - 1] && tmp.num - 1 >= 0)
{
q[s].num = tmp.num - 1;
q[s++].t = tmp.t +1;
s %= LEN;
vis[tmp.num - 1] = 1;
} if (!vis[tmp.num * 2] && tmp.num <= k)
{
q[s].num = tmp.num * 2;
q[s++].t = tmp.t +1;
s %= LEN;
vis[tmp.num * 2] = 1;
}
} return -1;
} int main()
{
int x,k; while (~scanf ("%d%d",&x,&k))
{
int ans = bfs (x,k); printf ("%d\n",ans);
}
return 0;
}
POJ 3278 Catch That Cow(BFS 剪枝)的更多相关文章
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 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 (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- poj 3278 Catch That Cow bfs
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj 3278 Catch That Cow(bfs+队列)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- leetcode 数据库十题记录
题目从难到易记录.解题过程中,如果不太熟悉,可以将题目中的表自己手动录入到自己的数据库中,就方便学习,测试. 185. Department Top Three Salaries 要求就是查询出每个部 ...
- [Python] Python Libs
The Python Standard Library has a lot of modules! To help you get familiar with what's available, he ...
- 项目: 基于Python socket模块实现的简单 ftp 项目:
需要 自己创建一个 info 文件 用来存储用户信息 服务器: import socket import pickle import struct import os import time ''.s ...
- reactor官方文档译文(2)Reactor-core模块
You should never do your asynchronous work alone. — Jon Brisbin 完成Reactor 1后写到 You should never do y ...
- 错误日志写入到本地磁盘(lock 队列)
今天照常在b站上看看编程:看到有讲错误日志处理的,自己没写过日志,就看看了看: 主要是在讲的时候牵扯到了队列和lock的知识点:这方面知识自己了解的更少,那就记录一下.
- 阿里云 django的一次web维护记录
首先, 丢给我一个阿里云的server的账号/password,之前没有玩过阿里云,想想应该也是ssh服务来远程登陆. 环境: centos+nginx+uwsgi+python2.7+django. ...
- 通过wireshark,以及python代码收发邮件,了解smtp协议,pop协议工作过程
40返回连接server成功 41.43发送ehlo命令查询server支持命令 返回250 44.46请求认证 server响应235认证成功 47.49发送mail命令发送者邮箱 返回250 ...
- 例说linux内核与应用数据通信(四):映射设备内核空间到用户态
[版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet.文章仅供学习交流,请勿用于商业用途] 一个进程的内存映象由以下几部分组成:代码段.数据段.BSS段和 ...
- 7.Maven之(七)pom.xml配置文件详解
转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...
- Spring中的AOP注解方式和XML方式
应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型 A. 每种通知的特点和使用方式 B. 获取各种数据,方便日后操作5. 执行表 ...