hdu3455 dp

Leap Frog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 548    Accepted Submission(s): 200


Problem Description
Jack and Jill play a game called "Leap Frog" in which they alternate turns jumping over each other. Both Jack and Jill can jump a maximum horizontal distance of 10 units in any single jump. You are given a list of valid positions x 1,x 2,…, x n where Jack or Jill may stand. Jill initially starts at position x 1, Jack initially starts at position x 2, and their goal is to reach position x n.Determine the minimum number of jumps needed until either Jack or Jill reaches the goal. The two players are never allowed to stand at the same position at the same time, and for each jump, the player in the rear must hop over the player in the front.
 

Input
The input file will contain multiple test cases. Each test case will begin with a single line containing a single integer n (where 2 <= n <= 100000). The next line will contain a list of integers x 1,x 2,…, x n where 0 <=x 1,x 2,…, x n<= 1000000. The end-of-fi le is denoted by a single line containing "0".
 

Output
For each input test case, print the minimum total number of jumps needed for both players such that either Jack or Jill reaches the destination, or -1 if neither can reach the destination.
 

Sample Input
   
   
   
   
6 3 5 9 12 15 17 6 3 5 9 12 30 40
 

Sample Output
   
   
   
   
3 -1
 

Source
2009 Stanford Local ACM Programming Contest
 

Recommend
zhengfeng
 
dp[i][j]  看作前面人在第i个位置是后面的距离其j的最小步数。
具体方程见代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int N=100005;
int dp[N][15];
int hash[N*10];
int num[N];
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        int i;
        memset(hash,-1,sizeof(hash));
        memset(dp,inf,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            int temp;
            scanf("%d",&temp);
            num[i]=temp;
            hash[temp]=i;
        }
        int j,k;
        dp[2][num[2]-num[1]]=0;
        for(i=2;i<=n;i++)
        {
            for(j=1;j<=9;j++)
            {
                if(j>num[i])
                    break;
                for(k=j+1;k<=10;k++)
                {
                    int tt=num[i]-j;
                    if(hash[tt]>=0)
                        dp[i][j]=min(dp[hash[tt]][k-j]+1,dp[i][j]);
                }
            }
        }
        int mm=inf;
        for(i=1;i<=10;i++)
            if(dp[n][i]<mm)
                mm=dp[n][i];
        if(mm==inf)
            printf("-1\n");
        else
            printf("%d\n",mm);
    }
    return 0;
}


你可能感兴趣的:(hdu3455 dp)