HDU2717Catch That Cow
2016-01-18 19:30
363 查看
简单的搜索题目,,渣渣在这里给大家展示一下
题目大意:简单的搜索题,即有三种走法,最后要求抓到那头奶牛
第一种走法:向前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;
}
题目大意:简单的搜索题,即有三种走法,最后要求抓到那头奶牛
第一种走法:向前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;
}
相关文章推荐
- FFT原理详解
- JAVA网络编程
- HDU1241Oil Deposits
- hdu 5596 GTW likes gt
- 通过邮编获取城市
- 异常初探(五)
- binder的调用流程
- UI课程(tableView)
- 正则表达式
- hadoop常用算法简单实例
- HBase应用:Table设计
- 关于图灵书籍源代码下载的总结
- 数据类型
- 关于FLASH、EEPROM、RAM、ROM等
- LINUX nfs服务
- #2006 - MySQL server has gone away 问题解决方法
- 写下来回头看,在游戏中隐藏线程思路r3
- 通过 adb命令发送广播
- ASP.NET Temporary ASP.NET Files 缓存 想起的可恶BUG
- 利用BAPI(BAPI_GOODSMVT_CREATE)投料消耗(261)销售订单E库存