HDU2717Catch That Cow

简单的搜索题目,,渣渣在这里给大家展示一下

题目大意:简单的搜索题,即有三种走法,最后要求抓到那头奶牛
第一种走法:向前1格
第二种走法:走到是本位置2倍的位置
第三种走法:向后一格

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10312 Accepted Submission(s): 3221

Problem 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
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.

代码:

include

include

include

using namespace std;

int a[100005],book[100005];
int bfs(int x,int y)
{
a[x]=0;
book[x]=0;
int num,sum;//num为所处的位置,sum为可以到达的位置
memset(a,0,sizeof(a));
memset(book,0,sizeof(book));
queueq;
q.push(x);
while(!q.empty())
{
num=q.front();
q.pop();
if(num==y)
{
break;
}
for(int i=1;i<=3;i++)
{
if(i==1)
{
sum=num+1;
}
if(i==2)
{
sum=num*2;
}
if(i==3)
{
sum=num-1;
}
if(sum>=0&&sum<=100000&&book[sum]==0)
{
book[sum]=1;
a[sum]=a[num]+1;
q.push(sum);
}
}
}
return a[y];
}

int main()
{
int m,n;
while(~scanf(“%d %d”,&m,&n))
{
printf(“%d\n”,bfs(m,n));
}
return 0;
}

你可能感兴趣的:(HDU2717Catch That Cow)