BZOJ 1207 [HNOI2004] AcWing2118 whack-a-mole

BZOJ 1207 [HNOI2004] Whacking a Mole

Links available for review
AcWing 2118 Whack-a-Mole

topic

Mole is a kind of animal that likes to dig holes very much, but every certain time, it still likes to poke its head out on the ground to get some air.

According to this feature, Niu wrote a whack-a-mole game:

In an n×n grid, at certain moments the mole will poke its head out of a certain grid to get some air.

You can control a robot to beat the mole. If the mole appears in a certain grid at time i and the robot is in the same grid, then the mole will be killed by the robot.

The robot can only move one space at a time or stay in place.

The movement of the robot refers to moving from the current grid to the adjacent grid, that is, from the grid with coordinates (i, j) to (i−1, j), (i+1, j), (i, j−1), (i, j+1) four grids, the robot cannot walk out of the entire n×n grid.

When the game starts, you can freely choose the initial position of the robot.

Now that you know when and where moles appear within a certain period of time, please write a program to make the robot kill as many moles as possible within this period of time.
 

input format

The first line of the file is n,m, where m represents the number of moles that appeared within this period of time.

There are three data t, x, y in each of the next m lines, indicating that at the tth moment after the start of the game, a mole appeared in the grid in the xth row and the yth column.

Data are given in order of increasing t. Note that multiple moles may appear at the same time, but only one mole may appear at the same time and place.
 

output format

Output a positive integer representing the maximum number of moles killed.
 

data range

1≤n≤1000,
1≤m≤10000
 

Input sample:

2 2
1 1 1
2 2 2

Sample output:

1

Ideas:

       At the beginning, I didn’t know how to deal with the time. I always thought about the two-dimensional position and directly dp, but if the time is added, the space may be larger, and it is not easy to write. After reading other people’s solutions, it is indeed good, so record it.
       A state f [ i ] f[i] can be definedf [ i ] means hitting theiiThe maximum score that can be obtained by i moles and some moles in front. Then the transfer equation is very simplef [ i ] = max ( f [ j ] + 1 ) f[i] = max(f[j]+1)f[i]=max(f[j]+1 ) , wherejjj is the mole that satisfies the condition. With this equation, the calculation becomes very simple.
       So what is this condition? We can enumerate the coordinates of the two gophers respectively. At this time, if the Manhattan distance between the i-th hamster and the j-th hamster is not greater than the time difference between them, it means that the j-th hamster can be hit after hitting the i-th hamster .

code show as below:

#include<bits/stdc++.h>

using namespace std;

const int N = 1000000; 

int f[N], t[N], x[N], y[N], mx; 
 
int main(){
    
    
	int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; ++i) {
    
    
	    scanf("%d%d%d", &t[i], &x[i], &y[i]);
	    f[i] = 1;
    } 
    for(int i = 2; i <= m; ++i){
    
    
        for(int j = i - 1; j >= 1; --j)
            if(fabs(x[i] - x[j]) + fabs(y[i] - y[j]) <= t[i] - t[j])
                f[i] = max(f[i], f[j] + 1);
        mx = max(mx, f[i]);
    }
    printf("%d\n", mx);
}

insert image description here

We can open an array for pruning. where mx [ i ] mx[i]m x [ i ] representative1 − i 1-i1i f [ i ] f[i] the maximum value of f [ i ]

#include<bits/stdc++.h>
 
using namespace std;

const int N = 1000000;

int f[N], t[N], x[N], y[N], mx[N]; 

int main(){
    
    
    int n,m;   
    scanf("%d%d", &n, &m); 
    for(int i = 1; i <= m; ++i) {
    
    
	    scanf("%d%d%d", &t[i], &x[i], &y[i]);
	    f[i] = mx[i] = 1;
    } 
    for(int i = 2; i <= m; ++i) {
    
    
        for(int j = i - 1; j >= 1; --j){
    
    
            if(f[i] > mx[j])  break; 
            if(abs(x[i] - x[j]) + abs(y[i] - y[j]) <= t[i] - t[j]) 
            	f[i] = max(f[i], f[j] + 1); 
        }
        mx[i] = max(f[i], mx[i - 1]);  
    }
    printf("%d",mx[m]); 
    return 0; 
}

insert image description here

Guess you like

Origin blog.csdn.net/xxmy7/article/details/115049473