Artillery Positions
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 21381 |
|
Accepted: 8290 |
Description
headquarters generals are planning to deploy their artillery units on the grid map of N*m. A n*m map consists of n rows of m columns, each of which may be mountainous (denoted by "H") or plain (denoted by "P"), such as. A maximum of one artillery unit can be arranged on each Goping terrain (no artillery units can be deployed on the mountain); an artillery unit is shown in the black area of the attack range on the map:
If you deploy an artillery unit on a plain that is identified in gray on the map, the black grid in the figure represents the area it can attack: two grids along the horizontal, two in the vertical. Other white meshes on the graph are not attacked. It can be seen from the diagram that the artillery's attack range is unaffected by the topography.
Now, the generals plan how to deploy artillery units, in the premise of preventing accidental injury (to ensure that no two artillery units can attack each other, that is, any artillery unit is not within the range of other artillery units), the maximum number of troops in the entire map area can be placed in the Army of artillery units.
Input
The first line contains two positive integers separated by a space, representing N and M, respectively;
In the next n rows, each row contains a continuous m-character (' P ' or ' H ') with no spaces in the middle. Represents the data for each row in the map sequentially. N <= 100;m <= 10.
Output
only one row, containing an integer k, indicates the maximum number of artillery units that can be placed.
Sample Input
5 4PHPPPPHHPPPPPHPPPHHP
Sample Output
6
Source
Noi 01
Title Link: http://poj.org/problem?id=1185
Topic: Chinese problem, title
Title Analysis: Classic pressure DP, now do, because the number of columns is very small, so we can put the row state with a binary compression, 1 indicates that the point of the gun, 0 is not put, because the H position can not be put, so consider, first of each row of the terrain state compression exists in the F array, followed by analysis of the feasible state of An artillery can attack to its left right two grid position, so obviously, the same line, can not appear two consecutive 1, that is 0011, can not be separated from one position 1, that is 0101, which is illegal, only at least two points is 1 is legal, such as 1001, For each row we also want to record the number of States corresponding to the 1, that is, how many artillery, then we look at the column, similar to the row, any row of three rows of the same column, there can only be one 1, because a artillery vertical attack range is also two, that its longitudinal state only related to the first two lines, so we remember Dp[i][j][k For line I state is J, the I-1 line state is k when the maximum number of artillery can be placed, then dp[i][j][k] = Dp[i-1][k][l] + stnum[i], (I >= 2) L represents the state of the I-2 line, Stnum[i] represents the number of artillery on line I, For the first second row we deal with it separately, starting with the third line, the DP is done directly, and the maximum value of the last Dp[n-1][i][j] is the answer
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 105;int STA [MAX], Stnum[max], F[max];int Dp[max][max][max];char s[max][15];int main () {int n, m, cnt = 0;SCANF ("%d%d", &n, &m ); for (int i = 0; i < n; i++) scanf ("%s", S[i]), memset (f, 0, sizeof (f)), and for (int i = 0; i < n; i++) for (int j = 0; J < ; M J + +) if (s[i][j] = = ' H ') f[i] + = (1 << j); for (int j = 0; J < (1 << m); j + +) {int tmp = J;IF (J & (J << ; 1)) | | (J & (J << 2))) Continue;while (TMP) {stnum[cnt] + = (tmp & 1); TMP >>= 1;} Sta[cnt + +] = j;} for (int i = 0; i < cnt; i++) {if (F[0] & Sta[i]) continue;dp[0][i][0] = Stnum[i];} for (int i = 0; i < cnt; i++) {if (F[1] & Sta[i]) continue;for (int j = 0; J < CNT; J + +) {if (F[0] & Sta[j]) continu E;if (Sta[i] & Sta[j]) continue;dp[1][i][j] = max (Dp[1][i][j], dp[0][j][0] + stnum[i]);}} for (int r = 2, r < N; r++) {for (int i = 0; i < cnt; i++) {if (F[r] & Sta[i]) continue;foR (Int j = 0; J < CNT; J + +) {if (F[r-1] & Sta[j]) continue;if (Sta[i] & Sta[j]) continue;for (int k = 0; k < CNT; k++) {if (F[r-2] & Sta[k]) continue;if ((Sta[i] & Sta[k]) | | (Sta[j] & Sta[k])) CONTINUE;DP[R][I][J] = max (Dp[r][i][j], dp[r-1][j][k] + stnum[i]);}}} int ans = 0;for (int i = 0; i < CNT; i++) for (int j = 0; J < CNT; J + +) ans = max (ans, dp[n-1][i][j]); if (n = = 0 | | m = = 0) printf ("0\n"); elseprintf ("%d\n", ans);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1185 Artillery positions (Classic in DP Classic)