HDU 3158 PropBot(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3158


Problem Description
You have been selected to write the navigation module for PropBot. Unfortunately, the mechanical engineers have not provided a lot of flexibility in movement; indeed, the PropBot can only make two distinct movements. It can either move 10 cm forward, or turn towards the right by 45 degrees. Each of these individual movements takes one second of time.
 
Input
Your module has two inputs: the Cartesian coordinates of a point on the plane that the PropBot wants to get as close to as possible, and the maximum number of seconds that can be used to do this. At the beginning of the navigation, the robot is located at the origin, pointed in the  +x direction.

The number of seconds will be an integer between 0 and 24, inclusive. Both the x and y coordinates of the desired destination point will be a real number between -100 and 100, inclusive.

The first entry in the input file will be the number of test cases,  t (0 <  t <= 100). Following this line will be  t lines, with each line containing three entries separated by spaces. The first entry will be the number of seconds PropBot has to get close to the point. The second entry is the  x-coordinate of the point, and the third entry is the  y-coordinate of the point.
 
Output
Your program must return the distance between the goal point and the closest point the robot can get to within the given time.

Your result should include at least one digit to the left of the decimal point, and exactly six digits to the right of the decimal point. To eliminate the chance of round off error affecting the results, we have constructed the test data so the seventh digit to the right of the decimal point of the true result is never a 4 or a 5.
 
Sample Input
  
  
2 24 5.0 5.0 9 7.0 17.0
 
Sample Output
  
  
0.502525 0.100505
 
Source

题意:

每次有两个操作:

1、直线距离走10cm;

2、向右转45°;

每一个操作耗时为一秒。问在给定的时间内,能走到的离目标点最近的距离?

PS:

DFS每一步的每个方向,记录出现的最短距离;


代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const double dd = sqrt(2.0)/2*10;

int ti;
double x,y;
double d;

double dis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

void dfs(double x1,double y1,int dir,int tt)
{
    double aa;
    dir %= 8;
    aa=dis(x1,y1,x,y);
    if(aa < d)
    {
        d = aa;
    }
    if(tt==ti ||(aa-10*(ti-tt)>d))//如果剩下的步数就算走直线都比当前的d大就不用往下搜了
    {
        return ;
    }
    if(dir == 0)//沿+x走10cm
    {
        dfs(x1+10,y1,dir,tt+1);
    }
    else if(dir == 1)//沿+x偏-y为45°方向走10cm
    {
        dfs(x1+dd,y1-dd,dir,tt+1);
    }
    else if(dir == 2)//沿-y走10cm
    {
        dfs(x1,y1-10,dir,tt+1);
    }
    else if(dir == 3)//沿-y偏-x为45°方向走10cm
    {
        dfs(x1-dd,y1-dd,dir,tt+1);
    }
    else if(dir == 4)//沿-x走10cm
    {
        dfs(x1-10,y1,dir,tt+1);
    }
    else if(dir == 5)//沿-x偏+y为45°方向走10cm
    {
        dfs(x1-dd,y1+dd,dir,tt+1);
    }
    else if(dir == 6)//沿+y走10cm
    {
        dfs(x1,y1+10,dir,tt+1);
    }
    else if(dir == 7)//沿+y偏+x为45°方向走10cm
    {
        dfs(x1+dd,y1+dd,dir,tt+1);
    }
    dfs(x1,y1,dir+1,tt+1);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lf%lf",&ti,&x,&y);
        d = dis(0,0,x,y);
        dfs(0,0,0,0);
        printf("%.6lf\n",d);
    }
    return 0;
}
/*
2
24 5.0 5.0
9 7.0 17.0

5
9 7.0 17.0
*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值