POJ-1661-Help Jimmy (DP, recursive)

link:

https://vjudge.net/problem/POJ-1661

Meaning of the questions:

"Help Jimmy" is done on the next game scene shown in FIG.

Scene comprising a plurality of different length and height of the internet. The ground is the minimum platform height of zero, unlimited length.

Jimmy mice began to fall at time 0 from somewhere higher than all platforms, it's always falling speed of 1 m / sec. When Jimmy falls on a platform, the player chooses to make it run left or right, it's running speed is 1 m / sec. When Jimmy went to the edge of the platform and began to continue to fall. Jimmy every drop of height should not exceed MAX meters, otherwise it will fall to their deaths, the game will end.

Design a program that calculates Jimmy in the end of the ground the earliest possible time.

Ideas:

Just remember to start thinking about search, Dp [i] [j] , to record a minimum platform i, j landing position, and then the super memory.
Because each location only the left and right, go left and go all the right to consider, in the past an introduction of the current item.

Code:

include

include

include

include

//#include <memory.h>

include

include

include

include

include <math.h>

include

include

include <assert.h>

include

include

include

define MINF 0x3f3f3f3f

using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 1e3+10;

struct Node
{
int l, r;
int h;
bool operator < (const Node& that) const
{
return this->h > that.h;
}
}node[MAXN];
int n, x, y, m;
int Dp[MAXN][2];

void CalLeft(int i)
{
int k = i+1;
while (k <= n && node[i].h - node[k].h <= m)
{
if (node[i].l >= node[k].l && node[i].l <= node[k].r)
{
Dp[i][0] = node[i].h-node[k].h+min(Dp[k][0]+abs(node[i].l-node[k].l), Dp[k][1]+abs(node[i].l-node[k].r));
return;
}
k++;
}
if (node[i].h - node[k].h > m)
Dp[i][0] = MINF;
else
Dp[i][0] = node[i].h;
return;
}

void CalRight(int i)
{
int k = i+1;
while (k <= n && node[i].h-node[k].h <= m)
{
if (node[i].r >= node[k].l && node[i].r <= node[k].r)
{
Dp[i][1] = node[i].h-node[k].h+min(Dp[k][0]+abs(node[i].r-node[k].l), Dp[k][1]+abs(node[i].r-node[k].r));
return;
}
k++;
}
if (node[i].h - node[k].h > m)
Dp[i][1] = MINF;
else
Dp[i][1] = node[i].h;
return;
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(Dp, 0, sizeof(Dp));
scanf("%d%d%d%d", &n, &x, &y, &m);
for (int i = 1;i <= n;i++)
scanf("%d%d%d", &node[i].l, &node[i].r, &node[i].h);
sort(node+1, node+1+n);
node[0].l = node[0].r = x;
node[0].h = y;
node[n+1].l = -20000, node[n+1].r = 20000;
node[n+1].h = 0;
for (int i = n;i >= 0;i--)
{
CalLeft(i);
CalRight(i);
}
printf("%d\n", min(Dp[0][0], Dp[0][1]));
}

return 0;

}```c++

```

Guess you like

Origin www.cnblogs.com/YDDDD/p/11665979.html