poj1185-Artillery positions (state compression DP)

Source: Internet
Author: User

Artillery positions
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25647 Accepted: 9892

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
1#include <map>2 3#include <cstdio>4 5#include <iostream>6 7#include <string.h>8 9#include <algorithm>Ten  One using namespacestd; A  - Const intmaxn= -; -  the intN,m,cnt,ans; -  - Charch; -  + inta[maxn+1]; -  + intvald[maxn+1][1<<Ten+1]; A  at intvaldcount[maxn+1]; -  - intdp[maxn+1][maxn+Ten][maxn+Ten]; -  - //map<int,int> ma[maxn+10]; -  in BOOLJudgeintx) { -     return(! (x& (x<<1)) &&! (x& (x<<2))); to } +  - intBitcount (unsignedintN) the { *Unsignedintc =0 ; $      for(c =0; N ++c)Panax Notoginseng     { -n &= (N-1) ; the     } +     returnC; A } the  + intMain () - { $scanf"%d%d",&n,&m); $          for(intI=1; i<=n;i++){ -              for(intj=1; j<=m;j++){ -scanf"%c",&ch); the                 if(ch=='H'){ -A[i]|= (1<< (J-1));Wuyi                 } the             } -         } Wu          for(intI=1; i<=n;i++){ -Cnt=0; About              for(intj=0; J<= (1&LT;&LT;M)-1); J + +){ $                 if((! (J&a[i])) &&judge (J)) { -vald[i][cnt]=J; -                     //ma[i][j]=cnt; -cnt++; A                 } +             } thevaldcount[i]=cnt-1; -         } $          for(intI=0; i<=valdcount[1];i++){ the             intx=vald[1][i]; the              for(intj=0; J<= (1<< (M)-1); J + +) dp[1][i][j]=Bitcount (x); the         } the          for(intI=0; i<=valdcount[2];i++){ -             intx=vald[2][i]; in             intq=Bitcount (x); the              for(intj=0; j<=valdcount[1];j++) { the                 inty=vald[1][j]; About                 if(! (X&y)) dp[2][i][j]=max (dp[2][i][j],q+Bitcount (y)); the             } the         } the          for(intI=3; i<=n;i++){ +              for(intj=0; j<=valdcount[i];j++){ -                    intx=Vald[i][j]; the                    intq=Bitcount (x);Bayi                    for(intk=0; k<=valdcount[i-1];k++){ the                       inty=vald[i-1][k]; the                      for(intH=0; h<=valdcount[i-2];h++){ -                             intz=vald[i-2][h]; -                            if(! (x&y) &&! (y&z) &&! (x&z)) { theDp[i][j][k]=max (dp[i][j][k],dp[i-1][k][h]+q); the                            } the                     } the                   } -             } the         } the          for(intI=0; i<=valdcount[n];i++){ the             intx=Vald[n][i];94              for(intj=0; j<=valdcount[n-1];j++) { theans=Max (ans,dp[n][i][j]); the             } the         }98printf"%d\n", ans); About     return 0; -}

poj1185-Artillery positions (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.