(简单) POJ 1278 Catch That Cow,回溯。
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?
也是典型的BFS的题目,我第一次设的数组最大是100000*10,然后过了,第二次100000+5,居然也过了。。。
这个题就是从出发点开始bfs,有三条路可以走,一知道发现牛为止。
代码如下:
- #include<iostream>
- #include<cstring>
- #include<queue>
- using namespace std;
- int N,K;
- int rem[];
- void bfs()
- {
- queue <int> que;
- int t;
- que.push(N);
- rem[N]=;
- while(!que.empty())
- {
- t=que.front();
- que.pop();
- if(t==K)
- return;
- if(t*<=&&rem[t*]==-)
- {
- rem[t*]=rem[t]+;
- que.push(t*);
- }
- if(t+<=&&rem[t+]==-)
- {
- rem[t+]=rem[t]+;
- que.push(t+);
- }
- if(t->=&&rem[t-]==-)
- {
- rem[t-]=rem[t]+;
- que.push(t-);
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(false);
- while(cin>>N>>K)
- {
- memset(rem,-,sizeof(rem));
- if(K<=N)
- cout<<N-K<<endl;
- else
- {
- bfs();
- cout<<rem[K]<<endl;
- }
- }
- return ;
- }
(简单) POJ 1278 Catch That Cow,回溯。的更多相关文章
- 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(简单一维广搜)
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: 46715 Accepted: 14673 ...
- 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+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 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: 109702 Accepted: 34255 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- Sql Server异常捕获 try catch
declare @num int begin try end try begin catch select ERROR_LINE() as 错误行数, ERROR_MESSAGE() as 错误消息 ...
- subversion-fundamental concepts
1.在使用svn 的时候,版本号version的问题一直困恼. 如在目录/app/controller上面右键选择查看该目录的 show log ,弹出的窗口显示的Revision log.单击每一条 ...
- linux cat more less head tail
cat 命令: cat filename 查看一个文件的内容cat[选项][文件]... -b 对非空白行进行编号,行号从1开始-n 和nl命令差不多,对所有行(包括空白行)进行编号输出显示-E ...
- js框架——angular.js(2)
1. 模块的利用扩充 模块的名称也可以当做变量使用,例如: <body ng-app> <label><input type="checkbox" n ...
- [LeetCode] Magical String 神奇字符串
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- Windows API 之 CreateThread、WaitForSingleObject(未完)
WaitForSingleObject Waits until the specified object is in the signaled state or the time-out interv ...
- hdu_5589_Tree(莫队+字典树)
题目连接:hdu_5589_Tree 题意:给你一棵树和一些边值,n个点n-1条边,一个m,q个询问,每个询问让你输出在[l,r]区间内任意两点树上的路径的边权异或的和大于m的点对数. 题解:这题很巧 ...
- C#排列组合类
//----------------------------------------------------------------------------- // // 算法:排列组合类 // // ...
- 基于ATmgea8单片机设计的加热控制系统(转)
源:http://blog.163.com/zhaojun_xf/blog/static/3005058020085102562729/ 1 引言 温度是工业生产中主要的被控参数之一,与之相关的各种温 ...
- idx_rebuild_diff_idx_l.sql
1. set echo on feedback on set timing on set time on set pagesize 1000 set linesize 150 spool 02_do_ ...