Poj 1185 Artillery Position status compression DP

Source: Internet
Author: User
 

G-Artillery Position

Time limit:2000 msMemorylimit:65536kb64bit Io format:% I64d & % i64u

Submit status

Description

The generals of the Headquarters intend to deploy their artillery troops on the grid map of N * m. A n * m map consists of N rows and M columns. Each grid of the map may be a mountain (represented by "H") or a plain (represented by "p ), for example. A maximum of one artillery unit can be deployed on each plain terrain (the artillery unit cannot be deployed on the mountain). The black area of an artillery unit in the attack range on the map is shown:


If an artillery force is deployed on the gray plain marked by the map, the Black Grid in the figure indicates the area to which it can attack: two grids along the horizontal left and right, the two cells are vertical and vertical. No other white mesh attacks can be found on the graph. The artillery attack range is not affected by the terrain.
Now, the generals plan how to deploy artillery troops to prevent accidental injuries (ensure that no two artillery troops can attack each other, that is, no artillery force is within the attack scope of other Artillery Forces.) the maximum number of Artillery Troops in our army can be placed in the whole map area.

Input

The first line contains two positive integers separated by spaces, representing N and m respectively;
In the next n rows, each row contains M consecutive characters ('P' or 'H') with no spaces in the middle. Represent the data of each row in the map in order. N
& Lt; = 100; M & lt; = 10.

Output

Only one row contains an integer k, indicating the maximum number of artillery troops that can be placed.

Sample Input

5 4

Phpp

Pphh

PPPP

Phpp

Phhp

Sample output

6

[Topic]

A matrix composed of a square. Each square can be represented by a scale gun with 0. It cannot be represented by 1 as a scale gun (the original question is represented by a letter), so that the largest cannon can be placed, the cannon and the cannon do not attack each other.

[Resolution] It can be found that the status of each line of the package is only related to the status of the previous line. Each line uses the State compression method, and 0 indicates that the package is not amplified, 1 indicates the amplifier. In the same way, the hardware conditions must be met first, that is, some places cannot be the amplifier, then, there cannot be two 1 distance less than two in each row (to ensure that they do not attack each other). These must be handled in advance. Then there is the State representation and transfer problem. Because it is related to the status of the first two rows, a three-dimensional array should be opened to indicate the state, the status of the current row can be transferred from the status of the first two rows. That is, if the status of the current row meets the constraints of the first two rows (not attacking each other with the first two lines of cannon ), then, the maximum value of the current row is the value of the previous state plus the number of 1 in the current State (the number of the current line of Anchor)

[State representation] DP [I] [J] [k] indicates the maximum number of artillery when the state of row I is K and the state of the I-1 is J.

[State transition equation] DP [I] [k] [T] = max (DP [I] [k] [T], DP [I-1] [J] [k] + num [T]); num [T] is the number of 1 in t state

[DP boundary condition] DP [1] [1] [I] = num [I] status I can meet the hardware conditions of the first line (note: here I refers to the I state, not a binary number, open an array to save the binary state)

(The above analysis is based on the user's hahaha)

 

 

Code:

# Include <cstdio> # include <cstring> # include <iostream> # define max (a, B) (a)> (B )? (A) :( B) using namespace STD; int DP [110] [70] [70]; // status char map [110] [20]; // Save the H-P diagram (Hill plain diagram) int STK [70], cur [110]; // The STK [] storage satisfies the status where the distance between the two is less than 2. // The status int num [110] After the image read by the cur storage is 0 and 1. // number of 1 in the stored state: int M, N; // Number of N rows of m column int CNT; // Number of cur STK [], starting from 1. That is, the number of States where 1 cannot be adjacent and cannot be separated. Int jcount (int x) // number the number of 1 in the binary of an integer x (for initialization) {int sum = 0; while (x) {sum ++; X & = (x-1);} return sum;} int isok (int x) // determine whether the status is legal {If (X & (x <1) // determine whether there are 1 adjacent return 0; If (X & (x <2 )) // determine whether there is 1 return 0; return 1;} void jinit () // find all possible valid States {int I; CNT = 0; int Total = 1 <m; // because there are m columns, you can use the binary number 0 <= State <(1 <m) to indicate these statuses for (I = 0; I <total; I ++) {If (isok (I) STK [++ CNT] = I; // note that CNT starts from 1, STK [0] is not saved} int main () {Int I, J, K, T, ans; while (scanf ("% d", & N, & M )! = EOF) {If (n = 0 & M = 0) break; jinit (); memset (DP, 0, sizeof (DP); memset (cur, 0, sizeof (cur); getchar (); for (I = 1; I <= N; I ++) {scanf ("% s ", map [I] + 1) ;}for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {If (Map [I] [J] = 'H') {cur [I] + = (1 <(J-1 ));}}} // initialize the status of the first line for (I = 1; I <= CNT; I ++) {num [I] = jcount (STK [I]); if (STK [I] & cur [1]) = 0) DP [1] [1] [I] = num [I] ;}for (I = 2; I <= N; I ++) // recursion starts from the second row {for (j = 1; j <= CNT; j ++) // select a status J {If (STK [J] & cur [I]) continue; For (k = 1; k <= CNT; k ++) // select a status K {If (STK [k] & STK [J]) continue; For (t = 1; t <= CNT; t ++) // select a state t {If (STK [J] & STK [T]) continue for the I-1 line; If (DP [I-1] [k] [T] =-1) continue; DP [I] [T] [J] = max (DP [I] [T] [J], DP [I-1] [k] [T] + num [J]) ;}}}ans = 0; for (I = 1; I <= N; I ++) {for (t = 1; t <= CNT; t ++) {for (j = 1; j <= CNT; j ++) {ans = max (ANS, DP [I] [T] [J]) ;}} printf ("% d \ n", ANS) ;}return 0 ;}

Note:

1. Bit operations have a low priority. Remember to enclose the brackets;

2. There are three for loops in the recurrence cycle starting from the second row. To find the status of row I, it is pushed by the first two rows, the I-1 line is determined by the I-2 line, so the nested triple loop

Is the outermost layer for the I row, the innermost layer for the I-1 line;

3. I used to struggle with this state transfer equation. Why isn't it like a 0 or 1 backpack looking for the greatest value based on whether to add the nth item?

F [I] [v] = max {f [I-1] [v], F [I-1] [V-C [I] + W [I]}

So why is the update of the best state not DP [I] [T] [J] = max (DP [I-1] [k] [T], DP [I-1] [k] [T] + sum [J]), you can actually think like this: F [I-1] [k] [T] is the best until the last line

Then it is equivalent to finding F [I] [k] [T] = max (F [I-1] [k] [T]). however, the maximum value is equivalent to f [I] [k] [T;

4. the loop of the last updated optimum number ANS is to take the maximum value of F [I] [k] [T] under all the unordered values.

5. Explain the binary operation:

(1) X & (x <1) is used to determine whether two adjacent ones exist. X <1 indicates that X shifts one digit to the left and then performs an operation with itself. In this case, if the result is 0, it indicates that 1 of X is not adjacent; if the result is not 0

There must be two 1 adjacent. Similarly, X & (x <2) is the distance between two adjacent two places. Each number is a binary representation. (2) X & = (x-1) number 1 in the number function, for an integer, I want to calculate the number of 1 .. I will use an example to tell you, for example, 6 .. SO 2 hexadecimal 110, then I start from the right, I want to remove the rightmost 1, while counter CNT ++, then (x-1) it will definitely cause the rightmost 1 to disappear and shift to the rightmost position (think about this sentence ),

For example, 6 = 110 after shift 5 = 101

110 ---- the rightmost 1 is missing
101 ---- the rightmost 1 is more rightmost ..

If X and the X-1 do and the operation at this time will certainly clear the X rightmost 1

This operation continues until X is 0. The complexity is only related to the number of 1 in X, and the speed is very fast.

 

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.