In an N * n matrix, each (x, y) Coordinate has a hole at any time point (from the previous time point to the current time point ), A mole's head may be located at any position. If a point is hit by a hammer, the range of the hammer activity is D (1 = <D <= 5) it is the coordinate point that can be reached in the circle with a radius and is moving in a straight line. The hammer can be moved to any position, and two moles cannot appear in the same place at the same time.
The appearance of moles in this case can obtain the maximum number of points.
Question: dynamic planning. The optimal result is obtained from the optimal result of the subproblem. Note: The Hammer can be moved out of the matrix.
How many points can be obtained for each point as an endpoints at a definite time point? In the next time, each point as an endpoints can be divided:
DP [I] [J] [k] = max {DP [x ~] [Y ~] [K-1] + mole [x] [y] [k]}
It indicates to (x ~, Y ~) It is the starting coordinate, and I, j is the target coordinate. In the previous moment (x ~, Y ~) The maximum score obtained is the score that can be obtained along the route;
Take the optional route and place the route with (I, j) as the target coordinate to obtain the maximum score at the K moment in DP [I] [J] [K].
The above is transferred from:
Http://wangjia007bond.blog.163.com/blog/static/304220242009102695054442/
Note: you do not need to place the hammer on the N * n game disk. It is possible that the hammer may fall out of the disk at a certain time point. At the next time, the hammer enters the disk along a straight line.
#include <cmath>#include <iostream>using namespace std;#define max(a,b) ( a > b ? a : b )bool map[32][32][11];int dp[32][32][11];int n, d, m;int gcd ( int a, int b ){return (a == 0) ? b : gcd ( b % a, a );}int get_sum ( int x1, int y1, int x2, int y2, int t ){int i, dx, dy, g, sum = 0;dx = x2 - x1; dy = y2 - y1;if ( dx * dx + dy * dy > d * d ) return 0;if ( dx == 0 && dy == 0 )return map[x2][y2][t];if ( dx == 0 ){if ( y1 > y2 ) swap(y1, y2);for ( i = y1; i <= y2; i++ )sum += map[x2][i][t];return sum;}if ( dy == 0 ){if ( x1 > x2 ) swap(x1, x2);for ( i = x1; i <= x2; i++ )sum += map[i][y1][t];return sum;}g = gcd ( abs(dx), abs(dy) );dx = dx / g; dy = dy / g;for ( i = 0; i <= g; i++ )sum += map[x1+i*dx][y1+i*dy][t];return sum;}int main(){int i, j, x, y, t, mt;int sx, tx, sy, ty, ans;while ( scanf("%d%d%d",&n,&d,&m) ){if ( n + d + m == 0 ) break;memset(map,0,sizeof(map));memset(dp,0,sizeof(dp));ans = mt = 0;for ( i = 1; i <= m; i++ ){scanf("%d%d%d",&x,&y,&t);map[x+d][y+d][t] = 1;if ( t > mt ) mt = t;}n = n + 2 * d;for ( t = 1; t <= mt; t++ ){for ( x = 0; x < n; x++ ){for ( y = 0; y < n; y++ ){sx = x - d < 0 ? 0 : x - d;tx = x + d >= n ? n - 1 : x + d;sy = y - d < 0 ? 0 : y - d;ty = y + d >= n ? n - 1 : y + d;for ( i = sx; i <= tx; i++ ){for ( j = sy; j <= ty; j++ ){if ( (i-x)*(i-x) + (j-y)*(j-y) <= d*d ) dp[x][y][t] = max ( dp[x][y][t], get_sum ( i, j, x, y, t ) + dp[i][j][t-1] );}}if ( t == mt && dp[x][y][t] > ans ) ans = dp[x][y][t];}}}printf("%d\n",ans);}return 0;}