Poj1185 Artillery Position (State compression)

Source: Internet
Author: User

Artillery positions
Time Limit: 2000 MS Memory Limit: 65536 K
Total Submissions: 15261 Accepted: 5743

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 4
PHPP
PPHH
PPPP
PHPP
PHHPSample Output

6 Source

Noi 01
I am very excited to compress a state file by myself. However, it takes longer time for me to write it than others. I hope someone else can give me a suggestion.
Memory: 1872 KBTime: 422 MSLanguage: CResult: Accepted

<SPAN style = "FONT-FAMILY: Times New Roman"> # include <stdio. h> struct recor {int r [65], num, s [65];} a [110]; // a [I] stores all status int dp [110] [65] [65], tem, n, m that meet the horizontal non-overlapping requirements of line I; int max (int I, int j) {return I> j? I: j;} void pb (int I) {int j, x, d; for (j = 0; j <1 <m; j ++) {if (! (J & tem) </SPAN> <SPAN style = "FONT-FAMILY: 'times New Roman '"> // </SPAN> <SPAN style =" FONT-FAMILY: 'times New Roman' "> ① if it is 1, the plain 0 indicates the mountains, this step cannot identify the status that meets the conditions. For Beginners, manually simulate bit operations to understand the meaning of this sentence </SPAN> <SPAN style = "FONT-FAMILY: times New Roman "> {if (j & (j <1) | (j & (j <2 ))) // This is the case where one line of artillery can attack the other party. During bit operations, pay attention to the priority of bit operations, and add brackets </SPAN> <SPAN style = "FONT-FAMILY: 'times New Roman '"> continue; a [I]. r [a [I]. num] = j; // save the state d = j; x = d % 2; while (D = (d> 1) {x + = d % 2;} a [I]. s [a [I]. num ++] = x; // x indicates the number of guns corresponding to each status }}int main () {int I, j, sum, k, p, max; char c; while (scanf ("% d", & n, & m )! = EOF) {for (I = 0; I <n; I ++) {a [I]. num = 0; tem = 0; getchar (); for (j = 0; j <m; j ++) {c = getchar (); if (c = 'P') tem = (tem <1); else tem = (tem <1) + 1; // This is to convert the image into a binary tem, where 0 represents the plain and 1 represents the mountain. I didn't understand why I didn't set the plain to 1, in this case, 1 means that 0 means that no is more in line with our normal thinking? But I will find the problem only when I write it like that. for details, refer to the label ①} pb (I) ;}for (I = 1; I <n; I ++) for (j = 0; j <a [I]. num; j ++) for (p = 0; p <a [I-1]. num; p ++) dp [I] [j] [p] = 0; // initialize for (j = 0; j <a [0]. num; j ++) for (p = 0; p <a [0]. num; p ++) dp [0] [j] [p] = a [0]. s [j]; // It is also initialized for (j = 0; j <a [1]. num; j ++) // The for (p = 0; p <a [0]. num; p ++) if (! (A [1]. r [j] & a [0]. r [p]) dp [1] [j] [p] = dp [0] [p] [0] + a [1]. s [j]; for (I = 2; I <n; I ++) // each row {for (p = 0; p <a [I]. num; p ++) // all States of each row {max = 0; for (k = 0; k <a [I-1]. num; k ++) // The status of the previous row {for (j = 0; j <a [I-2]. num; j ++) // The status of the first two rows {if ((! (A [I-1]. r [k] & a [I-2]. r [j]) & (! (A [I]. r [p] & a [I-2]. r [j]) & (! (A [I]. r [p] & a [I-1]. r [k]) // determine whether there is overlap before and after the three rows {if (dp [I] [p] [k] <dp [I-1] [k] [j] + a [I]. s [p]) dp [I] [p] [k] = dp [I-1] [k] [j] + a [I]. s [p]; // This is the maximum value, when n = 1, N-1 is 0. for (I = 0; I <a [n-1]. num; I ++) for (j = 0; j <a [N-2]. num; j ++) {if (max <dp [n-1] [I] [j]) max = dp [n-1] [I] [j];} else for (j = 0; j <a [0]. num; j ++) {if (max <dp [0] [j] [0]) max = dp [0] [j] [0];} printf ("% d \ n", max) ;}return 0 ;}</SPAN>

 

Related Article

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.