Poj-1185 Artillery Position

Source: Internet
Author: User

Http://poj.org/problem? Id = 1185

Artillery positions
Time limit:2000 ms   Memory limit:65536 K
Total submissions:18687   Accepted:7204

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
Analysis: At the beginning, the two-dimensional array is always stuck, but the upper state is always unavailable, but the third-dimensional array is used later. DP [I] [J] [k] indicates row I, the state of J, K is the state of the I-1, the most majority of the first line I.
State transition equation:
DP [I] [J] [k] = max (DP [I] [J] [K], DP [I-1] [k] [r] + sum [J]); r is the state of the I-2 line, which is recursive in turn.
You can learn how many 1 functions a decimal number is converted into a binary number.
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int sta [4096], n, m, p; int sum [4096]; int DP [102] [65] [65]; int max (int x, int y) {If (x> Y) return X; else return y ;} int count (int n) {int num = 0; while (n) {N & = (n-1); num ++;} return num ;} // convert a single decimal number to the number of 1 functions in the binary number. Void Init () {int I; for (I = 0; I <1 <m; I ++) {if (I <1 & I) continue; if (I <2 & I) continue; if (I> 1 & I) continue; if (I> 2 & I) continue; sum [p] = count (I); STA [p ++] = I ;}} int fit (INT X, int y) {If (X & Y) return 0; else return 1;} int main () {char STR [200] [20]; int I, j, a [200], K, R; scanf ("% d", & N, & M); P = 0; memset (DP, 0, sizeof (DP); memset (A, 0, sizeof (a); memset (sum, 0, sizeof (SUM); memset (STA, 0, sizeof (STA); for (I = 1; I <= N; I ++) {getchar (); For (j = 1; j <= m; j ++) {scanf ("% C ", & STR [I] [J]); If (STR [I] [J] = 'H') A [I] + = 1 <m-J ;}} init (); for (I = 0; I <p; I ++) {If (FIT (STA [I], a [1]) {DP [1] [I] [0] = sum [I] ;}} for (I = 0; I <p; I ++) {If (FIT (STA [I], a [2]) {for (j = 0; j <p; j ++) {If (FIT (STA [J], a [1]) {If (FIT (STA [J], Sta [I]) {DP [2] [I] [J] = max (DP [2] [I] [J], DP [1] [J] [0] + sum [I]) ;}}}for (I = 3; I <= N; I ++) {for (j = 0; j <p; j ++) {If (! FIT (STA [J], a [I]) continue; For (k = 0; k <p; k ++) {If (! FIT (STA [J], Sta [k]) continue; If (! FIT (STA [K], a [I-1]) continue; For (r = 0; r <p; r ++) {If (! FIT (STA [J], Sta [R]) continue; If (! FIT (STA [R], a [I-2]) continue; If (! FIT (STA [R], Sta [k]) continue; DP [I] [J] [k] = max (DP [I] [J] [K], DP [I-1] [k] [r] + sum [J]) ;}}} int ans = 0; for (I = 0; I <p; I ++) for (j = 0; j <p; j ++) if (ANS <DP [N] [I] [J]) ans = DP [N] [I] [J]; printf ("% d \ n", ANS); Return 0 ;}

 

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.