hdu 3455 Leap Frog
2011-10-19 22:29
183 查看
[align=left]Problem Description[/align]
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
x1,x2,…, xn where Jack or Jill may stand. Jill initially starts at position x1, Jack initially starts at position x2, and their goal is to reach position xn.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.
[align=left]Input[/align]
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 x1,x2,…, xn
where 0 <=x1,x2,…, xn<= 1000000. The end-of-fi le is denoted by a single line containing "0".
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=101000;
int n;
int a[maxn];
int f[maxn][12];//dp[i][j]表示i位置,i+j位置的两个人
int main()
{
while(scanf("%d",&n)==1&&n)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(f,-1,sizeof(f));
f[1][1]=0;
for(int i=2;i<=n;i++)
{
for(int j=1;i+j<=n&&a[i+j]-a[i]<=10;j++)
{
for(int k=1;i-k>=1&&a[i+j]-a[i-k]<=10;k++)
{
if(f[i-k][k]==-1) continue;
if(f[i][j]==-1) f[i][j]=f[i-k][k]+1;
else f[i][j]=min(f[i][j],f[i-k][k]+1);
}
}
}
int ans=-1;
for(int i=n-1;i>=1&&a
-a[i]<=10;i--)
{
if(f[i][n-i]==-1) continue;
if(ans==-1) ans=f[i][n-i];
else ans=min(ans,f[i][n-i]);
}
printf("%d\n",ans);
}
return 0;
}
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
x1,x2,…, xn where Jack or Jill may stand. Jill initially starts at position x1, Jack initially starts at position x2, and their goal is to reach position xn.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.
[align=left]Input[/align]
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 x1,x2,…, xn
where 0 <=x1,x2,…, xn<= 1000000. The end-of-fi le is denoted by a single line containing "0".
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
6 3 5 9 12 15 17 6 3 5 9 12 30 40
[align=left]Sample Output[/align]
3 -1
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=101000;
int n;
int a[maxn];
int f[maxn][12];//dp[i][j]表示i位置,i+j位置的两个人
int main()
{
while(scanf("%d",&n)==1&&n)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(f,-1,sizeof(f));
f[1][1]=0;
for(int i=2;i<=n;i++)
{
for(int j=1;i+j<=n&&a[i+j]-a[i]<=10;j++)
{
for(int k=1;i-k>=1&&a[i+j]-a[i-k]<=10;k++)
{
if(f[i-k][k]==-1) continue;
if(f[i][j]==-1) f[i][j]=f[i-k][k]+1;
else f[i][j]=min(f[i][j],f[i-k][k]+1);
}
}
}
int ans=-1;
for(int i=n-1;i>=1&&a
-a[i]<=10;i--)
{
if(f[i][n-i]==-1) continue;
if(ans==-1) ans=f[i][n-i];
else ans=min(ans,f[i][n-i]);
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- HDU 3455 Leap Frog
- HDU 3455 Leap Frog(线性DP)
- HDU 3455 Leap Frog (线性dp)
- hdu 3455 Leap Frog
- HDU 3455 Leap Frog(线性DP)
- HDU 3455
- HDU 2222 Keywords Search AC自动机 可做模板
- http://acm.hdu.edu.cn/showproblem.php?pid=2604
- hdu 4339 线段树 单点更新
- HDU 1081
- HDU Big Event in HDU - 多重背包
- hdu 2616
- hdu 4612 Warm up
- hdu 1708 Fibonacci String 斐波那契数列
- HDU 3452 Bonsai
- hdu 1181 DFS
- hdu3502,状态压缩dp
- hdu 4849
- HDU_4952_number transformation_数学题
- HDU 5029 Relief grain