POJ1185: Artillery (reduction of the country)

Source: Internet
Author: User

Description

Ordered the general to n*m the grid. Their artillery units were deployed on the map. A n*m the map consists of n rows of M-columns, each of which can be a mountain (denoted by "H") or plain (denoted by "P"), for example. 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 seen in the black area of the attack on the map:

Assuming that an artillery unit is deployed on a plain that is identified in gray on the map, the black grid in the figure represents the area to which it can attack: two grids along the landscape, two in each direction. 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 troops, in the premise of preventing accidental injury (to ensure that no two artillery units can attack each other, that is, no matter what one of the artillery units in the attack range of other artillery units), in the entire map area, the maximum number of troops to be placed in the Army's artillery units.

Input

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

Output

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

Sample Input

5 4PHPPPPHHPPPPPHPPPHHP

Sample Output

6
Basically the same topic as HDU4539, just a little bit different in the inference situation.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int N,m;char map[105][ 15];int mat[105][15];int now[1<<10],pre[1<<10],prepre[1<<10];int l_now,l_pre,l_prepre;int dp[1 <<10][1<<10],tem[1<<10][1<<10],tt[1<<10];void dfs (int id,int k,int p,int sum) {if (k        &GT;=M) {Now[++l_now] = p;        Tt[l_now] = sum;    Return } if (k>=2 && mat[id][k] &&! ( p& (1<< (k-2))) &&!        (p& (1<< (k-1)))) DFS (id,k+1,p| (    1<<k), sum+1); else if (k==1 && mat[id][k] &&! ( p& (1<< (k-1)))) Dfs (id,k+1,p| (    1<<k), sum+1); else if (k = = 0 && mat[id][k]) DFS (id,k+1,p| (    1<<k), sum+1); DFS (id,k+1,p,sum);}    void Solve () {int i,j,k,t;    TEM[1][1] = pre[1] = prepre[1] = 0;    L_pre = L_prepre = 1;        for (k = 0; k<n; k++) {l_now = 0;        DFS (k,0,0,0); for (i = 1; i<=l_now; i++)            for (j = 1; j<=l_pre; j + +) Dp[i][j] = 0;                for (i = 1; i<=l_now; i++) for (j = 1; j<=l_pre; j + +) for (t = 1; t<=l_prepre; t++)                    {if (Now[i] & prepre[t]) continue;                    if (Now[i] & Pre[j]) continue;                    if (Pre[j] & prepre[t]) continue;                DP[I][J] = max (dp[i][j],tem[j][t]+tt[i]);        } for (i = 1; i<=l_now; i++) for (j = 1; j<=l_pre; j + +) Tem[i][j] = Dp[i][j];        for (i = 1; i<=l_pre; i++) prepre[i] = Pre[i];        for (i = 1; i<=l_now; i++) pre[i] = Now[i];        L_prepre = L_pre;    L_pre = L_now;    }}int Main () {int i,j,ans;            while (~SCANF ("%d%d", &n,&m)) {for (i = 0; i<n; i++) {scanf ("%s", Map[i]); for (j = 0; j<m; j + +) {if (map[i][j] = = ' P ') matI                [j] = 1;            else mat[i][j] = 0;        }} solve ();        Ans = 0;        for (i = 1; i<=l_pre; i++) for (j = 1; j<=l_prepre; j + +) ans = max (ans,tem[i][j]);    printf ("%d\n", ans); } return 0;}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ1185: Artillery (reduction of the country)

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.