POJ - 3278 Catch That Cow
2017-05-26 20:54
316 查看
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?
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
Sample Output
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
传送门:http://poj.org/problem?id=3278
题意:农夫所在位置为N,0≤N≤100000,奶牛所在位置为K,0≤K≤100000,农夫和奶牛在同一条直线上,农夫每次移动可以向前走一步,或者退后一步,或者传送到当前坐标的2倍位置,比如从5到10,一步完成。奶牛并不知道农夫在抓它,所以一直在原地不动,问农夫最短可以用多少步抓到奶牛。
分析:一道BFS的题,但是DP也可以解,每次BFS都进行三次操作,前进一步,后退一步,传送到二倍位置,如果没有访问过,则标记访问,步数加一,直到找到解。因为农夫后退只能一步一步退,所以用DP的时候先从农夫的初始位置向前扫描,每移动一次步数加一直到到0位置。
再从初始位置向后扫描,后位置(dp[i])步数为前一位置步数+1(dp[i-1]+1),再计算从二分之一当前坐标传送到目前位置的步数,取最小值,注意细节处理。当i为奇数时dp[i]=min(dp[i],dp[i/2+1]+2);例如5需要3->6->5,一共两步操作,当i为偶数时dp[i]=min(dp[i],dp[i/2]+1);扫描完成后dp[奶牛初始位置]就为最终答案。
经过测试DP和BFS内存使用不相上下,但DP4ms就能AC,BFS需要32ms。
细节:第一次DP数组只开了100000,一直WA,后来发现是数组不够大,所以遇到这种类型的还是把数组多开几个单位比较保险。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 90830 | Accepted: 28508 |
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
传送门:http://poj.org/problem?id=3278
题意:农夫所在位置为N,0≤N≤100000,奶牛所在位置为K,0≤K≤100000,农夫和奶牛在同一条直线上,农夫每次移动可以向前走一步,或者退后一步,或者传送到当前坐标的2倍位置,比如从5到10,一步完成。奶牛并不知道农夫在抓它,所以一直在原地不动,问农夫最短可以用多少步抓到奶牛。
分析:一道BFS的题,但是DP也可以解,每次BFS都进行三次操作,前进一步,后退一步,传送到二倍位置,如果没有访问过,则标记访问,步数加一,直到找到解。因为农夫后退只能一步一步退,所以用DP的时候先从农夫的初始位置向前扫描,每移动一次步数加一直到到0位置。
for(i=start;i>0;i--) dp[i-1]=dp[i]+1;
再从初始位置向后扫描,后位置(dp[i])步数为前一位置步数+1(dp[i-1]+1),再计算从二分之一当前坐标传送到目前位置的步数,取最小值,注意细节处理。当i为奇数时dp[i]=min(dp[i],dp[i/2+1]+2);例如5需要3->6->5,一共两步操作,当i为偶数时dp[i]=min(dp[i],dp[i/2]+1);扫描完成后dp[奶牛初始位置]就为最终答案。
经过测试DP和BFS内存使用不相上下,但DP4ms就能AC,BFS需要32ms。
细节:第一次DP数组只开了100000,一直WA,后来发现是数组不够大,所以遇到这种类型的还是把数组多开几个单位比较保险。
/*DP*/
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAX = 1e5+5;
int start,target;
int now ,pos;
int dp[MAX];
int i;
int main()
{
cin >> start >> target;
for(i=start;i>0;i--) dp[i-1]=dp[i]+1;
for(i=start+1;i<=target;i++)
{
dp[i]=dp[i-1]+1;
if (i%2==1) dp[i]=min(dp[i],dp[i/2+1]+2);
else
dp[i]=min(dp[i],dp[i/2]+1);
}
cout << dp[target] <<endl;
return 0;
}
/*BFS*/ #include<iostream> #include<cstring> #include<queue> using namespace std; const int MAX=1E5+10; static bool visit[MAX]={false}; static int step[MAX]={0}; int now,position; int first,finally; queue <int> que; int BFS(int n) { que.push(n); while(!que.empty()) { now=que.front(); que.pop(); for(int i=0;i<3;i++) { if (i==0) position=now-1; else if(i==1) position=now+1; else position=2*now;//now 当前位置 position 经过一步移动后的位置 if(visit[position]==false&&position>=0&&position<=MAX) { visit[position]=true; step[position]=step[now]+1; que.push(position); } if(position==finally) return step[position]; } } } int main() { cin >> first >> finally; while(!que.empty()) que.pop(); memset(visit,false,sizeof(visit)); memset(step,0,sizeof(step)); if (first>=finally) cout << first-finally <<endl; else cout << BFS(first) << endl; return 0; }
相关文章推荐
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow 广度优先搜索
- POJ--3278 Catch That Cow
- POJ-3278-Catch That Cow[基础BFS]
- POJ 3278 Catch That Cow[BFS]
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow(基础题)
- POJ - 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow(第一道广搜题,(*^__^*) 嘻嘻……)
- POJ 3278 Catch That Cow <BFS>
- poj-3278-Catch That Cow
- poj 3278 Catch That Cow bfs
- Catch That Cow POJ - 3278 图论模型 BFS
- poj 3278 Catch That Cow 优化深搜
- HDU 2717&&poj 3278 Catch That Cow
- POJ-3278-Catch That Cow
- POJ_3278 Catch That Cow(BFS)
- POJ 3278 - Catch That Cow
- poj 3278 Catch That Cow