传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10…
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<stdio.h> #include<string.h> #include<queue> using namespace std; ]; struct node { int x,step; }; int bfs(int n,int k) { if(n==k) ; queue<n…
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次走出三步n-1,n+1,n*2入队,直到最后找到答案为止.要注意: ①n不能变为负数,负数无意义,且无法用数组记录状态会runtime error ②n不用大于100000 代码: #include<cstdio> #include<cstring> #include<queue…
题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS 但是要注意 设置 数组的范围 小心 RE AC代码 #include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cmath> #inclu…
题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置. 并且农夫有三种移动方式,X + 1,X - 1,X * 2.问最少几步抓到牛. 開始觉得非常easy的,三方向的BFS就能顺利解决.然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前.牛早跑了. 然后反应过…
题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxn 100005 using namespace…
相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include<iostream> #include<queue> #include<cstring> #include<cstdio> using namespace std; const int maxn=100001; bool vis[maxn]; int step[max…
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <cmath> #include <cstring> using namespace std…
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K 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…
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catch That Cow(POJ3278) BFS入门题,然鹅我还是WA了四五发,因为没注意,位置0是可以访问的.再者就是初始位置在push之后,要标记为已经访问. 图片挺不错,我们地大(武汉)的旖旎风光,放松一下. 题目链接:POJ3278 Description Farmer John has be…
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 124528   Accepted: 38768 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. H…
Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 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,00…
Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 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,00…
Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 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,00…
Catch That Cow 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…
Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 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,00…
Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 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,0…
题目链接:http://poj.org/problem?id=3278 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…
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 Jo…
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 Jo…
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 Jo…
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=2e5+3; int n,k; int dp[max…
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; ],vis[]; queue<int>q; int bfs(int n,int k) { int head,next,i; q.push(n); a[n]=; vis[n]=; ) { head=q.front(); q.pop(); ;i<;i++) {…
农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; queue<int> q; ], arr[]; void go(int n, int k) { q.push(n); vis[n…
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 m…
题意: 0到N的数轴上,每次可以选择移动到x-1,x+1,2*x,问从n移动到k的最少步数. 思路: 同时遍历三种可能并记忆化入队即可. Tips: n大于等于k时最短步数为n-k. 在移动的过程中可能会越界.重复访问. poj不支持<bits/stdc++.h>和基于范围的for循环. #include <iostream> #include <queue> using namespace std; const int M=110000; struct P{int x…
注:本人英语很渣,题目大意大多来自百度~=0= 题目大意 农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在N (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ≤ 100,000) ,约翰下次可以从x移动到x+1或者x-1或者2*x的地方,问约翰最少需要多少步才能找到他的牛.   用BFS可以解决~ 不过需要剪枝 不然会MLE  值得一提的一点是步数不要用结构体保存   或许是我剪枝不够好  用结构体保存会MLE   #include <iostream>…
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以广搜到的最短的路不一定是所要的路线 //所以应该把所有的路径都搜索出来,找到最短的转折数,看他是不是不大于2 //我是 用边搜索边更新当前路径的最小转弯数 来写的 #include<stdio.h> #include<string.h> #include<math.h> #…
这题的思想很简单,就是每次找出队列里面花费时间最少的来走下一步,这样当我们找到k点后,所花费的时间一定是最少的. 但要用一个标记数组vis[200010],用来标记是否走过.否则会内存溢出. #include<queue> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; ]; struct Point{ int position,Time; Point(…
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> ; using namespace std; int n,m; struct data { int x,step; } p; ]; void bfs() { memset(vis,,sizeof(vis)); //重置 que…