Poj 1185 Artillery Position (status compression DP entry question)

Source: Internet
Author: User
Artillery positions
Time limit:2000 ms   Memory limit:65536 K
Total submissions:17272   Accepted:6593

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 <= 100; m <= 10.

Output

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

Sample Input

5 4PHPPPPHHPPPPPHPPPHHP

Sample output

6


Idea: State compression, which uses binary to indicate the state of each location, and then enumerative. It means to use 1 and 0 for each row to indicate whether the positions are "P" or "H", and then use MP [I] To save the binary value and convert it to the decimal number.
Then we enumerate all the cases.
DP [I] [J] [k] indicates row I, current J status, the I-1 line is the most canonicalized number DP in K State [I] [J] [k] = max (DP [I] [J] [K], DP [I-1] [k] [p] + sum [J]). you must understand the details ..
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <string> # include <algorithm> # include <cstdlib> # include <set> # include <queue> # include <stack> # include <vector> # include <map> # define n 100010 # define mod 10000007 # define lson l, mid, idx <1 # define rson Mid + 1, R, idx <1 | 1 # define LC idx <1 # define RC idx <1 | 1 const double EPS = 1e-11; const double Pi = ACOs (-1.0 ); const Double E = 2.718281828; Type Def long ll; const int INF = 1000010; using namespace STD; int MP [102]; // records the status of each row. For example, hpph is expressed as 1001, MP [I] records MP [I] = 9 = 2 ^ 3 + 2 ^ 0; int sum [103], CNT [102]; /// sum records the number of 1 in each case, while CNT records the corresponding int n, m, Len, DP [110] [110] [110]; bool OK (INT X) // determine whether there is a conflict in the case of X, that is, whether one or both of the left and right overlaps with one {If (X & (x <1 )) | (X & (x <2) return 0; return 1 ;}int getsum (int x) // X that meets the condition, calculate the number of 1 {int ans = 0; while (x> 0) {If (X & 1) // when the last digit is 1, ANS ++; x> = 1; // shift one digit left, that is Determine whether the next digit is a} return ans;} void finds () // preprocessing 0 ~ 2 ^ all cases of m {memset (CNT, 0, sizeof CNT); Len = 0; For (INT I = 0; I <(1 <m ); I ++) // I enumerate the binary numbers of all M bits {If (OK (I) {sum [Len] = getsum (I ); CNT [Len ++] = I ;}} int main () {// freopen ("in.txt", "r", stdin ); while (CIN> N> m) {char C; getchar (); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {scanf ("% C", & C); If (C = 'H ') MP [I] | = (1 <j); //} getchar ();} finds (); memset (DP,-1, sizeof DP ); for (INT I = 0; I <Len; I ++) // process the first line first {If (! (CNT [I] & MP [0]) DP [0] [I] [0] = sum [I];} /// the next step is the recurrence process for (INT I = 1; I <n; I ++) {for (Int J = 0; j <Len; j ++) {If (MP [I] & CNT [J]) continue; For (INT II = 0; II <Len; II ++) // I-1 row {If (CNT [J] & CNT [II]) continue; For (int jj = 0; JJ <Len; JJ ++) // I-2 row {If (CNT [JJ] & CNT [J]) continue; If (CNT [JJ] & CNT [II]) continue; if (DP [I-1] [II] [JJ] =-1) continue; DP [I] [J] [II] = max (DP [I] [J] [II], DP [I-1] [II] [JJ] + sum [J]) ;}}} int ans = 0; For (INT I = 0; I <Len; I ++) for (Int J = 0; j <Len; j ++) if (ANS <DP [n-1] [I] [J]) ans = DP [n-1] [I] [J]; cout <ans <Endl;} return 0 ;}




Poj 1185 Artillery Position (status compression DP entry question)

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.