Question Link
In a two-dimensional Cartesian coordinate system, there are n × n holes. The coordinates of each hole are (x, y), 0 ≤X,Y<N,Give you a hammer to hit the hamster. In the first place, you can place the hammer anywhere. If you were in (x1, Y1) for a second ), the distance between the integer (X2, Y2) that the next second moves in a straight line is less than or equal to D, and when the hammer moves (X2, Y2, all the points in a straight line at two points can be hit. For example, (0, 0) Move to (0, 3 ). If (0, 1), (0, 2) a mouse appears, it will be hit. Find the mouse that can play the most.
Idea: DP [I] [J] [k] indicates the maximum number of points (I, j) in K seconds. Equal to DP [x] [y] [k-1] (point (X, Y) is a point that can reach (I, j) in any second) + number of hamster displayed in a straight line determined by two points. Calculate the maximum value.
1 // 3034 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <iostream> 6 # include <algorithm> 7 8 using namespace STD; 9 10 int Mapp [30] [30] [20]; 11 int DP [30] [30] [20]; 12 int n, D, M; 13 14 int gcd (int A, int B) 15 {16 return (A = 0 )? B: gcd (B % A, A); 17} 18 19 int getsum (INT Sx, int Sy, int ex, int ey, int T) 20 {21 if (SX = ex & Sy = ey) return Mapp [SX] [sy] [T]; // The same point 22 int dx = ex-Sx, dy = ey-sy; 23 int sum = 0; 24 if (dx = 0) // if the two vertices are in the same row 25 {26 if (SY> ey) Swap (SY, ey); 27 for (INT I = sy; I <= ey; I ++) 28 sum + = Mapp [SX] [I] [T]; 29 return sum; 30} 31 else if (DY = 0) // The Same Column 32 {33 If (SX> ex) Swap (sx, ex); 34 for (INT I = SX; I <= E X; I ++) 35 sum + = Mapp [I] [sy] [T]; 36 return sum; 37} 38 else39 {40 int G = gcd (ABS (dx), ABS (dy); 41 dx/= g; 42 dy/= g; 43 for (INT I = 0; I <= g; I ++) // all the points on the diagonal line 44 sum + = Mapp [dx * I + SX] [dy * I + Sy] [T]; 45 return sum; 46} 47} 48 int main () 49 {50 while (CIN> N> D> m) 51 {52 If (n = 0 & D = 0 & M = 0) break; 53 int X, Y, T, TT = 0; 54 memset (DP, 0, sizeof (DP); 55 memset (MAPP, 0, sizeof (MAPP ); 56 for (INT I = 0; I <m; I ++) 57 {58 CIN> x> Y> T; 59 Mapp [x + D] [Y + D] [T] = 1; 60 TT = max (TT, T); 61} 62 N + = 2 * D; // because the hammer can reach the outside of the disk at a certain time. 63 for (INT T1 = 1; t1 <= tt; T1 ++) 64 for (INT I = 0; I <n; I ++) 65 for (Int J = 0; j <n; j ++) 66 {67 int SX = max (I-d, 0 ); 68 int Sy = max (J-D, 0); 69 int EX = min (I + d, n-1); 70 int ey = min (n-1, J + d); 71 for (INT x = SX; x <= ex; X ++) 72 for (INT y = sy; y <= ey; y ++) 73 If (X-I) * (X-I) + (Y-j) * (Y-j) <= D * D) 74 DP [I] [J] [T1] = max (DP [x] [y] [t1-1] + getsum (X, Y, I, j, T1 ), DP [I] [J] [T1]); 75} 76 int Maxx = 0; 77 for (INT I = 0; I <n; I ++) 78 for (Int J = 0; j <n; j ++) 79 Maxx = max (DP [I] [J] [TT], Maxx ); 80 printf ("% d \ n", Maxx); 81} 82 return 0; 83}
View code