Help Jimmy-DP dynamic programming

"Help Jimmy" is a game completed on the scene shown in the figure below.

The scene includes multiple platforms with different lengths and heights. The ground is the lowest platform, with zero height and unlimited length.

Jimmy mouse starts to fall from somewhere higher than all platforms at time 0, and its falling speed is always 1 m/s. When Jimmy falls on a certain platform, the player chooses to let it run left or right, and it runs at a speed of 1 meter per second. When Jimmy ran to the edge of the platform, he continued to fall. Jimmy cannot fall more than MAX meters, or he will fall to death and the game will end.

Design a program to calculate the earliest possible time when Jimmy hits the ground.
The
first line of Input is the number of test data groups t (0 <= t <= 20). The first line of each group of test data is four integers N, X, Y, MAX, separated by spaces. N is the number of platforms (not including the ground), X and Y are the horizontal and vertical coordinates of where Jimmy starts to fall, and MAX is the maximum height of a fall. Each of the next N lines describes a platform, including three integers, X1[i], X2[i] and H[i]. H[i] represents the height of the platform, and X1[i] and X2[i] represent the horizontal coordinates of the left and right end points of the platform. 1 <= N <= 1000, -20000 <= X, X1[i], X2[i] <= 20000, 0 <H[i] <Y <= 20000 (i = 1...N). The units of all coordinates are meters.

The size of Jimmy and the thickness of the platform are ignored. If Jimmy happens to fall on the edge of a platform, it is considered to be on the platform. All platforms are not overlapping or connected. The test data guarantees that the problem must be solved.
Output
outputs an integer for each set of input test data, the earliest possible time when Jimmy hits the ground.
Sample Input
1
3 8 17 20
0 10 8
0 10 13
4 14 3
Sample Output
23
When Jimmy falls on the platform, there are two choices, left and right, and then continue to the next platform, and face the same problem, dp[i][j], j = 0, 1 (dp[i][ 0] represents the shortest time from the left of platform i to the ground, dp[i][1]] represents the shortest time from the right of platform i to the ground), the state transition equation is as follows:

For every board, there are two situations: left and right. Therefore, find the time for both cases when it goes to the left and when it goes to the right. Then compare and return the smaller one.

State transition equation:
dp[i][1]=a[i].ha[k].h+min(dp[k][0]+a[i].x2-a[k].x1,dp[ k][1]+a[k].x2-a[i].x2); //right

dp[i][0]=a[i].h-a[k].h+min(dp[k][0]+a[i].x1-a[k].x1,dp[k][1]+a[k].x2-a[i].x1); //左
Note: You must sort the platforms first, and treat the original Jimmy position as a platform, and the ground is also a platform;

#include<stdio.h>
#include<algorithm>
using namespace std;
int dp[1010][5]; 
typedef struct
{
    
    
	int x1,x2,h;
}S;
int n,x,y,m;
S a[1010];
bool cmp(S x,S y)//从大到小排序 
{
    
    
	return x.h>y.h;
}
void L(int i)//左边
{
    
    
	int k=i+1;
	while(k<n+1&&a[i].h-a[k].h<=m)
	{
    
    
		if(a[i].x1>=a[k].x1&&a[i].x1<=a[k].x2)
		{
    
    
			dp[i][0]=a[i].h-a[k].h+min(dp[k][0]+a[i].x1-a[k].x1,dp[k][1]+a[k].x2-a[i].x1 );
			return ;
		}
		k++;
	}
	if(a[i].h-a[k].h>m)//不能到达下一平台
	dp[i][0]= 0x3f3f3f; //无穷大
	else//直接落地 
	dp[i][0]=a[i].h;
 } 
 void R(int i)//右边 
{
    
    
	int k=i+1;
	while(k<n+1&&a[i].h-a[k].h<=m)
	{
    
    
		if(a[i].x2>=a[k].x1&&a[i].x2<=a[k].x2)
		{
    
    
			dp[i][1]=a[i].h-a[k].h+min(dp[k][0]+a[i].x2-a[k].x1,dp[k][1]+a[k].x2-a[i].x2 );
			return ;
		}
		k++;
	}
	if(a[i].h-a[k].h>m)//不能到达下一平台
	dp[i][1]= 0x3f3f3f;//无穷大
	else//直接落地 
	dp[i][1]=a[i].h;
} 
int main()
{
    
    
	int t,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
    
    
		a[0].x1=-20000,a[0].x2=20000,a[0].h=0;//大地 
		scanf("%d%d%d%d",&n,&x,&y,&m);
		a[1].x1=x,a[1].x2=x,a[1].h=y;//初始位置 
		for(i=2;i<=n+1;i++)
		scanf("%d%d%d",&a[i].x1,&a[i].x2,&a[i].h);
		sort(a,a+n+2,cmp);//从大到小排  
		for(i=n;i>=0;i--)//n+1是大地 
		{
    
    
		//从离地面最近的开始计算左右时间
			L(i);//计算左边 
			R(i);//计算右边 
		}
		printf("%d\n",min(dp[0][0],dp[0][1]));//最后输出从左时间短还是从右时间短
	}
}```

Guess you like

Origin blog.csdn.net/m0_46381590/article/details/113038834