POJ 3278&&2049&&3083】的更多相关文章

这次的题目叫图的深度&&广度优先遍历. 然后等我做完了题发现这是DFS&&BFS爆搜专题. 3278:题目是经典的FJ,他要抓奶牛.他和牛(只有一头)在一条数轴上,他们都站在一个点上(坐标为0~1e5).假设FJ的位置为x,他每次可以去x+1,x-1,x*2的地方.问他最少走几次才能抓到他的牛(牛不会动). 赤裸裸的BFS,注意判断是否越界(很容易RE) CODE #include<cstdio> #include<cstring> using na…
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最小花费时间恰好到达牛所在的地方. 思路:BFS求最优解,移动有三种情况,前后,和移动两倍位置,不过注意的地方是,当牛的坐标比你小,你只能一步步往后倒退,这个需要特判. #include<cstdio> #include<cmath> #include<cstring> #i…
题目传送门 /* 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…
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个单位,3. 移动到当前位置的二倍处.输出移动的最少次数. 解题思路 使用搜索,准确地说是广搜,要记得到达的位置要进行标记,还有就是减枝. 详情见代码实现. 代码实现 #include<cstdio> #include<cstring> #include<iostream>…
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool,基于一个考虑,多次循环时不用每次都清空visited,传递进去每次一个数字即可,比如第一次标记为1,判断也采用==1,之后递加即可. 2.设置一个node,用来记录相关参数和当前的步数,比如: struct node { int i; int j; int k; int s;//步数 }; 3.设计…
题目: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…
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…
题目链接: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…
题目链接:http://poj.org/problem?id=3278 ac代码: #include <iostream>#include <stdio.h>#include <queue>#include <string.h>#include <math.h>using namespace std;int a,b;int vis[100005];queue<int> q;int bfs(){ if(a>=b) return a…
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…
链接: http://poj.org/problem?id=3278 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 62113   Accepted: 19441 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a poi…
题目链接: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…
题目链接: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,在你计算出最少位置之前.牛早跑了. 然后反应过…
对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dungeon Master  http://poj.org/problem?id=2251 Input The input consists of a number of dungeons. Each dungeon description starts with a line containing th…
传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path.…
题意: 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…
The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6864   Accepted: 2375 Description There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and w…
传送门 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…
Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 78114   Accepted: 24667 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…
注:本人英语很渣,题目大意大多来自百度~=0= 题目大意 农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在N (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ≤ 100,000) ,约翰下次可以从x移动到x+1或者x-1或者2*x的地方,问约翰最少需要多少步才能找到他的牛.   用BFS可以解决~ 不过需要剪枝 不然会MLE  值得一提的一点是步数不要用结构体保存   或许是我剪枝不够好  用结构体保存会MLE   #include <iostream>…
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…
给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 //广搜,a是队列,step记录步数,vis记录哪些数被搜到过 #include<cstdio> #include<iostream> #define M 1000010 using namespace std; int step[M],a[M],vis[M]; int main() { ,tail=; scanf("%d%d",&n,&m); a[…
进一步了解了bfs; 题意:给你n,然后用+,-,*三种运算使n变成k; 漏洞:在算出新的数字之后,一定要判边界,否则RE,而且在每一步后面都得加判断是否等于K,如果是即刻退出,否则WA,判这个的时候需要顺序: 不过不明白为什么bfs这两个顺序为啥结果不同 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #define N 101000 using names…
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #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(…
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…
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…