poj185--artillery position (pressure DP)

Source: Internet
Author: User

Artillery positions
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20169 Accepted: 7805

Description

Headquarters generals are planning to deploy their artillery units on the grid map of N*m. A n*m map consists of n rows of m columns, each of which may be mountainous (denoted by "H") or plain (denoted by "P"), such as. A maximum of one artillery unit can be arranged on each Goping terrain (no artillery units can be deployed on the mountain); an artillery unit is shown in the black area of the attack range on the map:

If you deploy an artillery unit on a plain that is identified in gray on the map, the black grid in the figure represents the area it can attack: two grids along the horizontal, two in the vertical. Other white meshes on the graph are not attacked. It can be seen from the diagram that the artillery's attack range is unaffected by the topography.
Now, the generals plan how to deploy artillery units, in the premise of preventing accidental injury (to ensure that no two artillery units can attack each other, that is, any artillery unit is not within the range of other artillery units), the maximum number of troops in the entire map area can be placed in the Army of artillery units.

Input

The first line contains two positive integers separated by a space, representing N and M, respectively;
In the next n rows, each row contains a continuous m-character (' P ' or ' H ') with no spaces in the middle. Represents the data for each row in the map sequentially. N <= 100;m <= 10.

Output

Only one row, containing an integer k, indicates the maximum number of artillery units that can be placed.

Sample Input

5 4PHPPPPHHPPPPPHPPPHHP

Sample Output

6

Pressure DP, each line has a 2^m state, and then exclude the non-conforming, the distance between adjacent two is greater than or equal to 2, with J & J << 1 J & J << Second, the state of each row is determined by the last two lines, so the DP contains two lines of state, in order to be a DP pushes to another DP,DP[I][J][K] indicates that at line I, the state is the optimal solution for the j,i-1 line state of K.


#include <cstdio> #include <cstring> #include <algorithm>using namespace std; int map[120][12], state[    120][100], num[120]; char str[120][12]; int dp[120][100][100]; int sum (int x,int m) {int temp = 0, I;    for (i = 0; i < m; i++) if ((1<<i) & x) temp++; return temp;}    int main () {int n, m, I, J, K, L;    scanf ("%d%d", &n, &m);    Memset (Dp,0,sizeof (DP));    memset (num,0,sizeof (num));    for (i = 1; I <= n; i++) scanf ("%s", Str[i]);                for (i = 1; I <= N, i++) for (j = 0; J < m; J + +) {if (str[i][j] = = ' P ')            MAP[I][J] = 1;        else map[i][j] = 0;    } num[0] = 1;    State[0][0] = 0;    int x = 1 << m; for (i = 1; I <= N, i++) {for (j = 0; J < x; j + +) {if (!) ( j& (j<<1)) &&!            (j& (J&LT;&LT;2)))   {for (k = 0; k < m; k++)             {if (!                Map[i][k] && (j&1<<k) > 0) break;            } if (k = = m) state[i][num[i]++] = j;    }}} for (i = 0; i < num[1]; i++) {dp[1][i][0] = SUM (state[1][i],m);            } for (i = 2; I <= N, i++) for (j = 0; J < Num[i]; j + +) for (k = 0; k < num[i-1]; k++)                {if (state[i-1][k]&state[i][j]) continue;                        for (l = 0; l < num[i-2]; l++) {if (State[i-2][l]&state[i][j])                    Continue;                Dp[i][j][k] = max (dp[i][j][k],dp[i-1][k][l]);            } dp[i][j][k] + = SUM (state[i][j],m);    } int ans = 0;    for (i = 0; I <= num[n], i++) for (j = 0; J < num[n-1]; J + +) ans = max (ans,dp[n][i][j]); printf ("%d\n", ans) ; return 0;}


poj185--artillery position (pressure 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.