HDU 3158--PropBot【DFS】

PropBot

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 200    Accepted Submission(s): 63


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
 

广搜没做出来,在网上看的别人的深搜。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

double ch=sqrt(2.0)/2*10;
double minx;
double edx,edy;
int t;

double dis(double x1, double y1, double x2, double y2){

    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void dfs(double x,double y,int t,int dir){
    double D=dis(x,y,edx,edy);
    minx=min(minx,D);
    dir=dir%8;
    if(t==0 || (D-10*t>minx)) return ;
    if(dir==0) dfs(x+10,y,t-1,dir);
    else if(dir==1) dfs(x+ch,y-ch,t-1,dir);
    else if(dir==2) dfs(x,y-10,t-1,dir);
    else if(dir==3) dfs(x-ch,y-ch,t-1,dir);
    else if(dir==4) dfs(x-10,y,t-1,dir);
    else if(dir==5) dfs(x-ch,y+ch,t-1,dir);
    else if(dir==6) dfs(x,y+10,t-1,dir);
    else if(dir==7) dfs(x+ch,y+ch,t-1,dir);
    dfs(x,y,t-1,dir+1);
}
int main (){
    int n;
    scanf("%d",&n);
    while(n--){
        cin>>t>>edx>>edy;
        minx=dis(0,0,edx,edy);
        dfs(0,0,t,0);
        printf("%.6lf\n",minx);
    }
    return 0;
}


你可能感兴趣的:(HDU 3158--PropBot【DFS】)