Title Description
The mole is a kind of animal that likes digging a hole very much, but every time, it still likes to poke his head out to the ground to breathe. According to this feature, Ah Q wrote a game of hitting the mole: in a n*n grid, at some point the mole would try to breathe through a grid. You can control a robot to hit a mole, and if I moment the mole appears in a grid and the robot is in the same grid, the mole will be killed by the robot. And the robot can only move one cell at a time or stay where it is. The movement of a robot is to move from the current grid to the adjacent mesh, that is, from the coordinates (I,J) of the grid (I-1, J), (I+1, J), (I,j-1), (i,j+1) Four meshes, the robot cannot walk out of the entire n*n grid. At the beginning of the game, you are free to select the initial position of the robot. Now you know the time and place where the mole appeared during a period of time, hoping you would write a program that would allow the robot to kill as many moles in this period of time. input
The first behavior n (n<=1000), M (m<=10000), where m represents the number of moles appearing in this period of time, and the next m row has three data per row time,x,y indicates that there is a mole at the beginning of the game time, A mole appeared in the grid of the first X row. Time is given in ascending order. Note that there may be many moles at the same time, but only one mole may appear at the same time in the same place. Output
Contains only a positive integer representing the maximum number of killed Moles sample input 2 2
1 1 1
2 2 2
Sample Output
1
Problem: True and water
Almost out of the algorithm, for every mole, violence to find the first mole who is, and record this maximum, recorded as F[i] can, such a time complexity of O (m^2), is actually 10000*10000/2=5000w, is said to have been jammed, fog
A little bit of optimization, each time the search from the back to the front, the current optimal value (that is, the maximum value) > currently find the first few, it is not possible to better, directly exit, so the constant is more excellent, easy water over
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int N,m,i,j,ans;
int f[10005];
int x[10005];
int t[10005];
int y[10005];
int abs (int x) {
if (x<0)
return-x;
return x;
}
int main () {
scanf ("%d%d", &n,&m);
for (i=1;i<=m;i++) {
f[i]=1;
scanf ("%d%d%d", &t[i],&x[i],&y[i]);
for (j=i-1;j>=1;j--) {
if ((ABS (X[j]-x[i) +abs (Y[j]-y[i])) <=abs (T[j]-t[i])) {
if (F[j]+1>f[i])
f[i]=f[j]+1;
}
if (f[i]>j) break
;
}
Ans=max (Ans,f[i]);
printf ("aaa%d\n", ans);
}
printf ("%d\n", ans);
return 0;
}