猎豹

wccmfc123 2010-11-05 10:23:53
数据结构与算法实验题 6.2 猎豹
★实验任务
动物世界里猎豹的奔跑总是很让人有速度的美感。这天,猎豹盯上了一只野猪,现在
要捉住它,由于猎豹很好的保护色,所以野猪没发现猎豹。猎豹和野猪在同一条直线上,
他们的坐标分别是 x 和 y,x 是小于等于 y 的。
现在猎豹有两种跑法:
1.
跑动一格,就是从x到x+1或者x-1;
2.
跑动一倍,就是从x到2*x。
现在请你找出最少要几步捉到野猪。
★数据输入
输入第1行给出两个正整数x、y(0<x<=y<=100000),分别表示猎豹的坐标和野猪的坐标。
★数据输出
输出只有 1 行,给出最少步数。
输入示例
输出示例
5 17 4


求解,,有没有好方法,我写的会超时啊
...全文
561 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
power721 2010-11-09
  • 打赏
  • 举报
回复
USACO OPEN07 Problem 'catchcow' Analysis
by Richard Ho
One way to view this problem is to consider each number on the number line as a vertex in a graph, and the edges connect each number to its adjacent numbers as well as twice that number. The problem then becomes the shortest path from N to K on that graph. Since all edges have equal weight (i.e. 1 minute) we can use a simple breadth first search so solve this problem. We visit each vertex in order of distance from the start, using a queue to see what to visit next. This gives us linear time solution. Here is a solution based on the description above.


#include <iostream>
using namespace std;

#define QSIZE 200000
int whereinqueue[QSIZE]; //index of element in queue, -1 if not in queue
int q[QSIZE],qstart,qend; //this is a circular array queue
int dist[QSIZE]; //dist[x] = distance to x from starting position
int n,k;

/*update x in queue, or add if not already in queue and dist==-1*/
void update(int x, int d)
{
if (whereinqueue[x]==-1 && dist[x]==-1)
{
dist[x]=d;
//add x to the end of queue
q[qend]=x; whereinqueue[x]=qend;
qend++; if (qend==QSIZE) qend=0;
}
}
void processqueue(void) /*process the element at the head of the queue*/
{
int x=q[qstart],d=dist[q[qstart]];
//we need to add all places we can reach to the queue
if (x+1<QSIZE) update(x+1,d+1);
if (x-1>=0) update(x-1,d+1);
if (2*x<QSIZE && x!=0) update(2*x,d+1);
//remove head of queue
whereinqueue[x]=-1;
qstart++; if (qstart==QSIZE) qstart=0;
}
int main(void)
{
int i;
scanf("%i %i",&n,&k);
for (i=0;i<QSIZE;i++)
{
whereinqueue[i]=-1;
dist[i]=-1;
}
q[0]=n;
dist[n]=0;
qstart=0;
qend=1; //start at point n
while(qstart!=qend)
processqueue();
printf("%i\n",dist[k]);
return 0;
}
power721 2010-11-09
  • 打赏
  • 举报
回复
POJ3278 Catch That Cow
BFS搜索
#include<iostream>
#include<queue>
using namespace std;

int a,b,f[200001]={0};
struct node
{int n,s;}t;
int bfs()
{
queue<node> q;
node st={a,0};
q.push(st);
f[a]=1;
while(!q.empty())
{
t=q.front();
if(t.n==b)
return t.s;
q.pop();
node ct={t.n,t.s+1};
if(t.n<b&&!f[2*t.n])
{
f[2*t.n]=1;
ct.n=2*t.n;
q.push(ct);
}
if(!f[t.n+1])
{
f[t.n+1]=1;
ct.n=t.n+1;
q.push(ct);
}
if(t.n-1>=0&&!f[t.n-1])
{
f[t.n-1]=1;
ct.n=t.n-1;
q.push(ct);
}
}
return 0;
}
int main()
{
scanf("%d%d",&a,&b);
printf("%d\n",bfs());
}
cug_fish_2009 2010-11-08
  • 打赏
  • 举报
回复
已经非常写得非常详细了。
大家去看看0/1背包的问题吧,
这个题是个典型的背包问题。
沪php攻城师 2010-11-08
  • 打赏
  • 举报
回复
我的想法是先算y%x得到余数,余数等于0则需要y/x步,不等于0将余数和x/2进行比较,小于等于一半的就是y/x加上余数的值为步数,否则就是y/x+1加上x减余数的值为步数。
Zjyouya 2010-11-08
  • 打赏
  • 举报
回复
你能再个个例子吗?你给的例子好像算不出来
Zjyouya 2010-11-08
  • 打赏
  • 举报
回复
路过 学习 能讲的详细点吗?
cug_fish_2009 2010-11-07
  • 打赏
  • 举报
回复
x、y的范围不大。用dp做。
dp(i,j)=1:表示第i步时可以走到j位置。
dp(i,j)=0:表示第i步时不可以走到j位置。

if(dp(i-1,j-1)==1 {j-1>=0} ||
dp(i-1,j+1)==1 {j+1<=100000} ||
dp(i-1,j*2)==1 {j*2<=100000} ||
dp(i-1,j/2)==1 {j%2==0})
则dp(i,j)=1。
否则dp(i,j)=0。
时间复杂度最大为o((y-x)*100000),空间复杂度o(100000*2)。
空间上可以用滚动数组。

如果用搜索,那么必须用记忆化搜索。
showjim 2010-11-07
  • 打赏
  • 举报
回复
直接算不知道怎么弄,感觉细节问题不好处理,写了一个log(n)的
private static int step(int x, int y)
{
int value = 0;
for (int y2, y0 = 0; x != y; value++)
{
y2 = y >> 1;
if (--y != x)
{
if ((y & 1) == 0)
{
value++;
if (x < y2)
{
if ((y0 = (y2 & 1)) == 0) y = y2;
else if (x == y2 - 1)
{
value++;
x = y;
}
else y = y2 + 1;
}
else
{
if (x != y2) value += Math.Min(y - x, x - y2) - 1;
x = y;
}
}
else if (x < y2)
{
y0 = 0;
y = y2;
}
else
{
value += Math.Min(y - x - y0, x - y2);
x = y;
}
}
}
return value;
}
绿色夹克衫 2010-11-06
  • 打赏
  • 举报
回复
只有2个方向,搜索就可以了,一种是从高位减下来,一种从低位加上去,其实几乎都可以直接算出来了。

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧