"POJ 3034" Whac-a-mole (DP)
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 3621 |
|
Accepted: 1070 |
Description
While visiting a traveling fun fair you suddenly has an urge to break the high score in the Whac-a-mole game. The goal of the Whac-a-mole game is to ... whack moles. With a hammer. The job easier you has first consulted the fortune teller and now you know the exact appearance patterns of the M Oles.
The moles appear out of holes occupying the N2 integer points (x,y) satisfying 0≤ x, y < n in a two-dimensional coordinate system. At each time step, some moles'll appear and then disappear again before the next time step. After the moles appear but before they disappear, you is able to move your hammer in a straight line to any position ( x2, y2) That's at the distance at the most d from your the current position ( x1, y1). For simplicity, we assume so can only move your hammer to a point having an integer coordinates. A Mole is whacked if the center of the hole it appears out of are located on the line between ( x1, y1) and ( x2, y2) (including the endpoints). Every mole whacked earns a point. When the game is starts, before the first time step, you is able to place your hammer anywhere you see fit.
Input
The input consists of several test cases. Each test case starts with a line containing three integersn, D and M, where n and d is as described above, and m was the total number of moles that would appear (1≤ n ≤ 20, 1≤ D ≤5, and 1≤ m ≤1000). Then follow m lines, each containing three integers x, y and T giving the POSIT Ion and time of the appearance of a mole (0≤ x, y < n and 1≤ T ≤10). No. Moles would appear at the same place at the same time.
The input is ended with a test case where n = D = m = 0. This case is should not being processed.
Output
For each test case output a single line containing a single integer, the maximum possible score achievable.
Sample Input
4 2 60 0 13 1 30 1 20 2 21 0 22 0 25 4 30 0 11 2 12 4 10 0 0
Sample Output
42
Source
Nordic 2006
...... For three days, it was finally suppressed. = =
The main problem: hit the hamster ... Have not played should have seen, the Gopher machine every second there will be several holes drilled out of the hamster, hit the more points higher.
First, give the n d m three numbers, respectively, the size of the Gopher machine (N*n matrix, the point is the hole) the maximum distance the player can move D this explains the number of rats
M-Gopher is given in X y t
Indicates that the first mouse is drilled from the X, Y hole in the first T-second.
The rule is this: every time you can move from x1,y1 to X2,y2, ask for a guaranteed two-point room.
LineDistance less than or equal to D
All the ground rats that were drilled in that second on the moving route will be hit.
Before the first second, the hammer can be in any position, and then the first second can only be moved by the position where i-1 second ends.
In other words, it is to find a T-second path, can meet the path to hit the largest number of hamsters.
Ahead of the high energy, directly on the train of thought, just to beg for a test instructions words to here to
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
————————————————————————————
The procedure is DP, which enumerates seconds and the start of the second hammer, and then the starting point is the center,
D is the radius, then all the points within the circle can be reached from this point in the second.
If you draw a circle, the code is not very good to write, you can change the way
Traverse all the points with this point and the distance d within the ordinate distance D, judging if out of range, jump out
As such traversal will only traverse farther.
Then a very pit very pit very pit very pit of place ...
, you is able to move your hammer in a straight line to any position (
x2,
y2)
Move your hammer in a straight line to any position
In a straight line to any position
to any position
Any position
Any
All right, if WA's going to do it ...
A set of data is attached:
20 5 41 0 10 1 10 5 21 6 2
(Discuss borrowed the
PS: Can not only negative direction beyond the positive can also exceed n!!!
The way to handle this is to move 0~n to 5~n+5.
Then 0~5 said -5~0 N+5~n+10 said it was going to burst
The code is as follows:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm > #include <map> #include <set> #define LL long long#define Pr pair<int,int> #define FREAD () freopen (" In.in "," R ", stdin) #define FWRITE () freopen (" Out.out "," w ", stdout) using namespace std;const int INF = 0x3f3f3f3f;const int MSZ = 10000;const int mod = 1e9+7;const double eps = 1e-8;int mp[12][33][33];int dp[12][33][33];int tmp[33][33];int dirx[] = {1, 1,-1,-1};int diry[] = {1,-1, 1,-1};int d,n;int dis (int x1,int y1,int x2,int y2) {return (X1-X2) * (X1-X2) + (y1-y2) * (y 1-Y2);} void cal (int t,int x,int y) {int xx,yy;for (int i = 0; I <= D; ++i) for (int j = 0; J <= D; ++j) {if (!i &&!j) {DP [T] [x] [y] + = mp[t][x][y];tmp[x][y] = dp[t][x][y];DP [t+1][x][y] = max (dp[t+1][x][y],dp[t][x][y]);//printf ("%d:%d,%d%d\n", T, X,y,tmp[x][y]); continue;} if (Dis (x,y,x+I,Y+J) > D*d) continue;for (int k = 0; k < 4; ++k) {if (!i && Dirx[k] < 0) continue;xx = x+i*dirx[k];if (!j && Diry[k] < 0) Continue;yy = y+j*diry[k];if (xx < 0 | | xx >= n | | yy < 0 | | yy >= n) continue;int G = __GCD (ABS (XX-X), ABS (YY-Y)), tmp[xx][yy] = tmp[xx+ (x-xx)/g][yy+ (y-yy)/g]+mp[t][xx][yy];DP [t+1][xx][yy] = max (dp[t+1 ][XX][YY],TMP[XX][YY]);//printf ("%d,%d->%d,%d:%d->%d get:%d\n", x,y,xx,yy,tmp[xx+ (X-XX)/g][yy+ (y-yy)/G],DP [T+1] [XX] [Yy],mp[t][xx][yy]);}}} int main () {//fread ();//fwrite (); int M,x,y,t;while (~SCANF ("%d%d%d", &n,&d,&m) && (n+d+m)) {memset (Mp,0,sizeof (MP)); Memset (Dp,0,sizeof (DP)); int Tim = 0;n + = 10;while (m--) {scanf ("%d%d%d", &x,&y,&t); Mp[t] [X+5] [Y+5] = 1;tim = max (tim,t);} for (int i = 1, I <= Tim; ++i) for (int j = 0, J < N; ++j) for (int k = 0; k < n; ++k) cal (i,j,k); int mx = 0;for (int i = 0; I < n; ++i) for (int j = 0; J < n; ++j) mx = max (mx,dp[tim+1][i][j]);p rintf ("%d\n", MX);} return 0;}
"POJ 3034" Whac-a-mole (DP)