hdu 4216 (dp 动态规划)

Computational Geometry?

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 700    Accepted Submission(s): 243


 

Problem Description

Computational geometry is a branch of computer science devoted to the study of algorithms which can be stated in terms of geometry. It often comes up with charming shapes and ideas.
In this problem, our poor princess is trapped in a castle by some bad guys again, yeah, again. So, let's seize the chance to be a hero.
Right now, the beautiful princess is in the original point of a Cartesian coordinate system, for simplification, the castle is treated as a coordinate system, like a common computational geometry problem.
There is a bomb which can be exploded anytime, and it locates at (Xo, Yo) in the castle. To save the princess, we need design a route for her to leave away the bomb as far as possible. But she already has a plan written on her notebook, which contains some vectors, and she insists on escaping in the vectors’ direction one by one, that is, if she is in point(0, 0), and the vector is (X, Y), she will be in point(X, Y) if she escapes in this vector.
You get her notebook now, and find princess's plan is a not a good plan sometimes. Then you decide to help the princess to make some slight modification, you can change the order of those vectors, and/or reverse some vectors, that is, change vector (X, Y) to vector (-X, -Y).
We want to know the maximum distance to the bomb after modification.

Input

The first line contains a single integer T, indicating the number of test cases. Each test case begins with three integers N, Xo, Yo. Then N lines following, each line contains two integers, Xi and Yi, indicating a vector.

Technical Specification
1. 1 <= T <= 100
2. 1 <= N <= 100
3. -100 <= Xi, Yi <= 100
4. -10 000 <= Xo, Yo <= 10 000

Output

For each test case, output the case number first, then the distance rounded to three fractional digits.

Sample Input

 

3 1 1 1 1 1 2 2 3 -1 2 1 -2 3 3 0 2 3 3 2 1 -1

Sample Output

 

Case 1: 2.828 Case 2: 7.000 Case 3: 9.849

Author

iSea@WHU

Source

首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛

对于该题,我们要找到的就是对于走完n个向量之后对应的x,y 。  那么我们可以通过dp1 [ i ][ x ]  表示当前的最大y值是多少

dp2 [ i ][ x ] 表示当前的最小值是多少。

代码: 

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N =105;
const int lim=10000;
const int inf=0x3f3f3f3f;

int dp1[105][20005];
int dp2[105][20005];
int bx,by;
int X[N],Y[N];
int n;
double maxx;

void solve(int x,int y)
{
    int tx=abs(x-bx);  int ty=abs(y-by);
    double dis=sqrt(tx*tx+ty*ty);
    maxx=max(dis,maxx);
    return ;
}

int main()
{
    int T;
    scanf("%d",&T);
    int kk=0;
    while(T--)
    {
        scanf("%d %d %d",&n,&bx,&by);
        bx+=lim;  by+=lim;
        for(int i=1;i<=n;i++){
            scanf("%d %d",&X[i],&Y[i]);
        }

        memset(dp1,-1,sizeof(dp1));
        memset(dp2,-1,sizeof(dp2));
        int tx,ty;
        int tmpx,tmpy;
        dp1[0][0+lim]=0+lim;  dp2[0][0+lim]=0+lim;
        for(int i=1;i<=n;i++)
        {
            for(int x=0;x<=20000;x++)
            {
                if(dp1[i-1][x]!=-1)
                {
                //cout<<"x  "<<x<<endl;

                tmpx=x+X[i];  tmpy=dp1[i-1][x]+Y[i];  // 正向求最大
                if(dp1[i][tmpx]==-1) dp1[i][tmpx]=tmpy;
                else dp1[i][tmpx]=max(dp1[i][tmpx],tmpy);
                //cout<<"tmpx "<<tmpx<<" tmpy "<<dp1[i][tmpx]<<endl;

                tmpx=x-X[i];  tmpy=dp1[i-1][x]-Y[i];  //反向求最大
                if(dp1[i][tmpx]==-1) dp1[i][tmpx]=tmpy;
                else dp1[i][tmpx]=max(dp1[i][tmpx],tmpy);

                //cout<<"tmpx "<<tmpx<<" tmpy "<<dp1[i][tmpx]<<endl;

                }
                if(dp2[i-1][x]==-1) continue;
                tmpx=x+X[i];  tmpy=dp2[i-1][x]+Y[i];
                if(dp2[i][tmpx]==-1) dp2[i][tmpx]=tmpy;
                else dp2[i][tmpx]=min(dp2[i][tmpx],tmpy);
                //cout<<"tmpx "<<tmpx<<" tmpy "<<dp2[i][tmpx]<<endl;

                tmpx=x-X[i];  tmpy=dp2[i-1][x]-Y[i];
                if(dp2[i][tmpx]==-1) dp2[i][tmpx]=tmpy;
                else dp2[i][tmpx]=min(dp2[i][tmpx],tmpy);
                //cout<<"tmpx "<<tmpx<<" tmpy "<<dp2[i][tmpx]<<endl;

            }
        }

        maxx=-inf*1.0;
        for(int x=0;x<=20000;x++)
        {
            if(dp1[n][x]!=-1){
                //cout<<"x "<<x<<endl;
                tmpx=x; tmpy=dp1[n][x];
                solve(tmpx,tmpy);
            }

            if(dp2[n][x]!=-1){
                //cout<<"x2 "<<x<<endl;
                tmpx=x; tmpy=dp2[n][x];
                solve(tmpx,tmpy);
            }
        }
        printf("Case %d: %.3f\n",++kk,maxx);

    }
    return 0;
}

/*

1
1 1 1
1 1


*/

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/81103475