Poj 1185 Artillery Position (State compression DP)

Source: Internet
Author: User

Artillery positions
Time limit:2000 ms   Memory limit:65536 K
Total submissions:19690   Accepted:7602

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

It's not easy. I spent one afternoon debugging...

# Include <iostream> # include <stdio. h> # include <math. h> # include <string. h >#include <algorithm> # include <iostream> using namespace STD; # define N 150 const int INF = 0x7fffffff; int map [N]; char G [N] [15]; int top, num [N], CNT [N]; int DP [N] [65] [65]; int getnum (int x) // obtain the number of cannons in a placement mode {int T = 0; while (x) {If (X & 1) T ++; X >>=1 ;} return t;} void Inti (INT m) {int I; Top = 0; for (I = 0; I <(1 <m); I ++) {if (I & (I <1) continue; if (I &( I <2) continue; num [Top] = I; CNT [top ++] = getnum (I) ;}} int main () {int I, j, k, n, m; while (scanf ("% d", & N, & M )! =-1) {for (I = 0; I <n; I ++) scanf ("% s", G [I]); for (I = 0; I <n; I ++) {map [I] = 0; // obtain the information of each line that cannot be placed on the cannon. For (j = 0; j <m; j ++) {If (G [I] [J] = 'H') {map [I] | = (1 <j );}}} inti (m); memset (DP,-1, sizeof (DP); for (I = 0; I <top; I ++) {If (! (Map [0] & num [I]) DP [0] [I] [0] = CNT [I];} For (INT r = 1; r <N; r ++) // enumerate the remaining rows {for (I = 0; I <top; I ++) // each row can be combined with {If (Num [I] & map [R]) continue; For (j = 0; j <top; j ++) // each combination of the previous row (I-1) {If (Num [I] & num [J]) continue; For (k = 0; k <top; k ++) // enumerate each combination of I-2 rows {If (Num [I] & num [k]) continue; If (DP [r-1] [J] [k] =-1) continue; DP [r] [I] [J] = max (DP [r] [I] [J], DP [r-1] [J] [k] + CNT [I]);} // The current row does not conflict with row J or k} int ans = 0; for (I = 0; I <top; I ++) {for (j = 0; j <top; j ++) {ans = max (ANS, DP [n-1] [I] [J]);} printf ("% d \ n", ANS);} return 0 ;}





Poj 1185 Artillery Position (State compression DP)

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.