Assignment week13-C-TT reward (required)

topic

With everyone's tireless help, TT successfully completed all the mysterious tasks.
The mysterious man was very happy and decided to give TT a reward, which is a game of picking up cats in daydreaming.
The cat picking game is like this. The cat falls from the sky and only falls within the range of [0, 10]. The specific coordinate range is shown in the figure below.

Insert picture description here

TT initially stands at position 5 and can only catch a falling cat within a range of no more than one meter per second. If it does not catch it, the cat will run away. For example, in the first second, TT can only receive cats in one of the three positions four, five, and six.
TT, who loves cats, wants to catch as many cats as possible. Can you help him?

Input

Multiple sets of examples. Enter an m (0 <m <100000) for each set of examples, which means there are m cats.
In the next m rows, each row has two integers ab (0 <b <100000), which means that a cat fell on point a at the b-th second.
Note that multiple cats may fall at the same point in the same second. When m = 0, the input ends.

Output

Output an integer x, which represents the maximum number of cats that TT can catch.

Sample Input

6
5 1
4 1
6 1
7 2
7 2
8 3
0

Sample Output

4

Ideas

This question requires calculating the maximum number of cats that can be obtained based on the time and location of the cat's appearance, as well as its own movement. You can use the idea of ​​dynamic programming to recurse the dp array in the order of time from back to front. Because it can only move one square at a time, the dp of the current position j is only affected by the dp values ​​of the three positions j-1, j, j+1, and because it is backwards, it is d[i][j] = dp[i][j]+max(dp[i+1][j-1],max(dp[i+1][j],dp[i+1][j+1]));Finally The answer is the starting position dp[0][5].

error

Note that when the new one dimension is updated, the problem of array access out-of-bounds may occur at the boundary of the array, which must be handled separately.

Code

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[100010][12],m,a,b,maxx;//能作为全局变量的尽量作为全局变量,否则有可能导致STACK_OVERFLOW 
int main()
{
    
    
	scanf("%d",&m);
	while(m!=0)
	{
    
    
		memset(dp,0,sizeof(dp));
		maxx=0;
		while(m--)
		{
    
    
			cin>>a>>b;
			if(maxx<b)
				maxx=b;
			dp[b][a]++;
		}
		for(int i=maxx-1;i>=0;i--)
		{
    
    
			for(int j=1;j<=10;j++)//注意 
				dp[i][j]+=max(max(dp[i+1][j],dp[i+1][j+1]),dp[i+1][j-1]);
			dp[i][0] += max(dp[i + 1][0], dp[i + 1][1]);							
		}				
		cout<<dp[0][5]<<endl;		
		cin>>m;	
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/alicemh/article/details/106225900