Poj 3034 DP

Source: Internet
Author: User

In the hamster game, you have a hammer. Every second you can hold a hammer to move the distance of D units. It must be a straight line. The mouse that passes through the mouse hole will be struck by a hammer, you must know the number and location of rats in each time period from the start. The problem is how many hamster you can hit from the beginning to the end of the game, at the beginning, the hammer can be in any position.


There is a trap in the question,

The only difference is that the coordinates of moles are positive, but the hammer Moving position is positive. It can be "any position". Therefore, the hammer can remove the matrix and then move it out of the matrix. Example:
20 5 4
1 0 1
0 1 1
0 5 2
1 6 2
0 0 0
The answer should be: 4

Change the N * n matrix to a (n + 2d) * (N + 2d) matrix. Add d to X and Y of all hamster, and DP in The Matrix.
We regard every second as a state, then, at the current time, the maximum number of rats that can be hit by a hammer at each position is the sum of the number of rats that can be moved to the position within one second and the number of rats that can be quickly hit at the starting status..

# Include "stdio. H "# include" string. H "# include" stdlib. H "struct node {int X, Y;} map [11] [0, 1010]; int temp [31] [31], DP [2] [31] [31], time [11]; int max (int A, int B) {if (a <B) return B; else return a;} int gcd (int A, int B) {If (B = 0) return a; else return gcd (B, A % B);} int CAL (INT X, int y, int Tx, int ty) // record the value generated from (x, y) to (TX, Ty) at a certain time {int dx, Dy, TP, ans; if (x = TX & Y = ty) return temp [x] [Y]; dx = TX-X; DY = ty-y; TP = GCD (ABS (dx), ABS (dy); dx/= TP; dy/= TP; ans = 0; For (X, Y; X! = TX | y! = Ty; x + = DX, Y + = Dy) ans + = temp [x] [Y]; ans + = temp [TX] [ty]; return ans ;} int main () {int N, D, M, X, Y, T, I, J, L, ANS, A, B; while (scanf ("% d", & N, & D, & M )! = EOF) {If (n + D + M = 0) break; memset (MAP, 0, sizeof (MAP); memset (time, 0, sizeof (time); While (M --) {scanf ("% d", & X, & Y, & T); time [T] ++; // record the total occurrence of time t. Map [T] [time [T]. X = x + D; // record the occurrence of time t for each hamster map [T] [time [T]. y = Y + D;} n + = d + D; memset (DP, 0, sizeof (DP); for (I = 1; I <= 10; I ++) {memset (temp, 0, sizeof (temp); For (j = 1; j <= time [I]; j ++) temp [map [I] [J]. x] [map [I] [J]. y] = 1; // specifies the time when I was recorded. For (j = 0; j <n; j ++) for (L = 0; L <N; l ++) // The enumerated position {DP [I % 2] [J] [l] = DP [1-I % 2] [J] [l]; for (A =-D; A <= D; A ++) for (B =-D; B <= D; B ++) // enumeration movement mode {If (A * A + B * B> D * D) continue; X = a + J; y = B + L; if (x <0 | x> = n | Y <0 | Y> = N) continue; DP [I % 2] [J] [l] = max (DP [I % 2] [J] [L], DP [1-I % 2] [x] [Y] + CAL (X, Y, J, L) ;}} ans = 0; for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) ans = max (ANS, DP [0] [I] [J]); printf ("% d \ n", ANS);} 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.