poj 3278 Catch That Cow 广搜
hdu 2717 Catch That Cow,题目链接
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11466 Accepted Submission(s): 3551
Problem Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
- Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
- Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
Source
USACO 2007 Open Silver
Recommend
teddy | We have carefully selected several similar problems for you: 2102 1372 1240 1072 1180
题意:给你两个位置k和n,求出从k到n的最小步数。
从k有三种方式变化方式: k+1,k-1,k*2。
队列广搜,一个vis数组查看是否访问过,一个step数组记录步数。
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string>
#include<iostream>
#include<string.h>
#include<math.h>
#define maxn 100005
using namespace std;
int vis[maxn];//标记是否访问过;
int step[maxn];//记录步数;
queue <int > q ;
int serch(int k,int n)
{int flag=0,a,mark=0;memset(vis,0,sizeof(vis));//数组清零;memset(step,0,sizeof(step));//数组清零;q.push(k); //将k入队;vis[k]=1; //标记为1;while(!q.empty()) //当数组非空时;{a=q.front(); //将队列首位赋值给a;q.pop(); //删除队列首位;for(int i=1; i<=3; i++){if(i==1)flag=a+1;else if(i==2)flag=a-1;elseflag=a*2;if(flag>100000||flag<0)//越界判断;continue;if(!vis[flag])//如果标记为0;{step[flag]=step[a]+1;//当前步数等于上次步数+1;vis[flag]=1;//标记为1;q.push(flag); //入队;if(flag==n)//如果当前值为n;return step[flag];//返回步数;}}}return 0;//如果是提交C++的话,要不要返回值都可以;//但如果是G++的话,必须要一个return 0;//这个是编译器的问题,具体也不是很清楚;
}
int main()
{int n,k; //k追n;while(~scanf("%d%d",&k,&n))//在这里我写成了k追n;{if(k>n) //如果k>n,只能通过不断减一得到,也就是k-n步;printf("%d\n",k-n);elseprintf("%d\n",serch(k,n));}return 0;
}
之前关于那个return 0,wa了,好坑,换了编译器就A了。
T^T,刚刚问了远航学长关于那个C++提交,G++提交不同的问题,结果学长也不知道,我也是第一次遇到这种问题,好忧桑~~
不过学长也说没见过,他说只是在这个oj上遇到的是 double要用 %.f 好坑的OJ~~QAQ
poj 3278 Catch That Cow 广搜相关推荐
- POJ 3728 Catch That Cow (广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42564 Accepted: 13225 ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30924 Accepted: 9536 D ...
- BFS POJ 3278 Catch That Cow
题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...
- poj 3278 Catch That Cow(广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45087 Accepted: 14116 ...
- POJ 3278 / hdu 2717 Catch That Cow (广搜)
POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...
- POJ 3278 Catch That Cow BFS
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32071 Accepted: 9866 D ...
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Catch That Cow——广搜
Catch That Cow Problem Description Farmer John has been informed of the location of a fugitive cow a ...
最新文章
- 你们考试,我们都有点紧张呢…
- 机器学习(五)PCA数据降维
- IndexTank全文检索引擎设计分析
- 大公司的资深工程师和小公司的Leader如何决择?
- java亚马逊模拟登录_java – 亚马逊MWS入门
- 网络软工个人作业4——Alpha阶段个人总结
- python实现微信打飞机
- linux解决Tomcat内存溢出
- win快速打开计算机,这十个“Windows+”使用技巧,打开你的电脑技巧新世界
- 【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
- 考研英语语法_Day05_状语和状语从句
- [angular1.6]Error: transition superseded ui-router 在angular1.6 报错误问题解决
- 第5-2课:图的拓扑排序
- 85D - Sum of Medians
- IE 10浏览器使用心得:界面简洁、功能很强大
- 了解模型预测控制2--什么是模型预测控制(MPC)
- 视频教程免费分享:嵌入式stm32项目开发之心率检测仪的设计与实现
- 自动化项目倍加福WCS-PG210E使用GSD文件
- JS数组的slice()方法传负数和字符串操作函数中的slice()、substr()、substring()
- 超市商品管理系统(c语言)
热门文章
- 免疫检查点 “新星“:HLA-E-MedChemExpress
- 关于洛谷p5705的解法c语言
- 运用ROS和OpenCV进行简单的图像处理之检测
- 基于SSM的网辩平台的设计与实现
- 检测触摸事件(用户触摸屏幕时提示信息)
- 1024程序员节,传智播客上海校区“智未来,益骑燃”公益骑行引爆上海张江高科...
- 20220519数据结构绿皮书读书笔记
- TypeScript(—)
- 如何让青少年不沉迷于手机,猿辅导:家长需做到这三点
- Java初级开发工程师工作的岗位职责(合集)