Dynamic Planning-monkey 2

Source: Internet
Author: User

If you are interested in hphp and monkeys, read monkey. However, after reading the questions, hphp is very stupid, if the monkey's scope of action is at the bottom of a horizontal column of n (n <= 100), what else should the pillar do? Simply use a wooden stake! From then on, hphp was determined to change the monkey's story. The monkey still lives on the pillar and takes the bug as a pleasure. Every time you sit down on a worm, he will be happy. But his happiness has limits, because he can only vertically move up and down on the same pillar, or horizontally move between adjacent pillars, and the moving time is 1 s (vertical or horizontal, the moving speed of every second is the same as that of the worm !), If the time t (0 <= t <= 1000) Monkey is on the column m (1 <= m <= n) k (0 <= k <= 100) and at this time, a worm occurs at the k position on m, so the monkey can sit on it, that is, add happiness. Now, when the monkey first (0 s) located at the bottom of Pillar 1 s (s is the top) cm, it is known that the time and column number of the insects are successively introduced, and the worm enters the pillar from the position of 0cm. Monkeys want to be the happiest. Can you still help them ~

Input

Multiple groups of test data, each group of test data first gives the integer n, t, s and m (0 <n <= 100, 0 <= t <=, 0 <= s <=) occupies one row. N indicates the total number of columns, t indicates the end time of the game, s indicates the maximum height of each column (cm), and t-time monkeys cannot sit on bugs. Next, there are m rows. Each row has two integers, a and B, indicating that a worm will appear at the bottom of Column B at the time of, and climb to the pillar at a speed of 1 cm/s. If it climbs out of the s point, the worm will be rescued!

Output

An integer is output for each group of test data, indicating the maximum number of happy monkeys.

Sample Input

3 4 3 3
0 2
1 1
2 3
3 4 4 4
0 1
1 1
0 2
1 2
1 5 4 4
0 1
1 1
2 1
3 1
1 5 3 4
0 1
1 1
2 1
3 1

Sample Output

1
0
1
2

Train of Thought Guide

(1) This question is very similar to the previous question. How can we deal with the problem of moving up and down?

(2) move up and down. Because the speed of moving insects and monkeys is the same, the monkeys do not need to move up or down at all. That is to say, if the monkeys move left or right on the s position, they will get an optimal value.

(3) if conditions are clearly determined, the optimal sub-structure is met, that is, it can be solved by using dp. It is similar to the question of "monkey.

Solution report

Mx [I] [j] indicates the greatest happiness value that can be obtained on the j pillar at the I time, grid [I] [j] indicates whether there are bugs in column j at the I time. So mx [I] [j] = Max (mx [I-1] [J-1], max [I-1] [j], max [I-1] [j + 1]) + grid [I] [j]; pay attention to the boundary. The key issue is how to find grid [j] [I. When a worm occurs on column B at the time of a, it should be a + s when it reaches Column B. Therefore, if a, B, grid [B] [a + s] = 1.

 

Source code

# Include <stdio. h>
# Define MAX (1 <29)
# Define N 110
# Define T 10100
# Define Max (a, B, c) (a)> (B )? (A) :( B)> (c )? (A)> (B )? (A) :( B) (c ))
Int mx [T] [N], grid [N] [T];
Int n, limt, len, m;
Int main ()
{
While (scanf ("% d", & n, & limt, & len, & m )! = EOF)
{
For (int I = 1; I <= n; I ++)
For (int j = 0; j <limt; j ++)
Grid [I] [j] = 0;
For (int I = 0; I <m; I ++)
{
Int a, B;
Scanf ("% d", & a, & B );
Grid [B] [a + len] = 1;
}
For (int I = 1; I <= n; I ++)
For (int j = 0; j <limt; j ++)
Mx [j] [I] =-MAX;
Mx [0] [1] = grid [1] [0];
For (int I = 1; I <limt; I ++)
{
For (int j = 1; j <= n & j <= I + 1; j ++)
{
Int a =-MAX, B =-MAX, c =-MAX;
If (j> 1)
A = mx [I-1] [J-1];
B = mx [I-1] [j];
If (j <n)
C = mx [I-1] [j + 1];
Mx [I] [j] = Max (a, B, c) + grid [j] [I];
}
}
Int mxmx = 0;
For (int I = 1; I <= n; I ++)
If (mx [limt-1] [I]> mxmx)
Mxmx = mx[ limt-1] [I];
Printf ("% d \ n", mxmx );
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.