POJ 1185 Artillery positions (state compression DP)

Source: Internet
Author: User

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 (with "P"



Input

The first line contains two positive integers separated by a space, representing N and M, and the next n rows, each containing a continuous m-character ('P' or 'H ' '  - .

Output

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

Sample Input

5 4 phpppphhppppphppphhp

Sample Output

6

Source

Noi State compression DP, similar to the previous poj3254
1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> - using namespacestd; the #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) - #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 - #defineN 106 + #defineM 16 A #defineINF 1e12 at intn,m; - CharMp[n][m]; - intCnt[n];//stored is the binary state of the given graph - intStk[n];//preprocessing all states that do not attack each other - intNum[n];//the number of 1 of STK stored - intDp[n][n][n];//Dp[i][j][k] indicates that the state at I is J, the maximum value when the state of I-1 is K in intTop//top indicates how many STK - BOOLOkintx) {//determine whether the state is legal, that is, there is no distance between adjacent 1 is less than 3 to     if(x& (x<<1))return false; +     if(x& (x<<2))return false; -     return true; the } * voidInit () {//Find all possible legal states, up to 60 kinds $top=0;Panax Notoginseng     intTotal=1<<m; -      for(intI=0; i<total;i++){ the         if(ok (i)) { +stk[top++]=i; A         } the     } + } - intCount_one (intx) {//Number of 1 in the binary of an integer x (for initialization) $     intans=0; $      while(x) { -ans+=x%2; -x>>=1; the     } -     returnans;Wuyi } the intMain () - { Wu      while(SCANF ("%d%d", &n,&m) = =2){ -         if(n==0&& m==0) About            Break; $            - init (); -          for(intI=0; i<n;i++){ -scanf"%s", Mp[i]); A           } +            the            for(intI=0; i<n;i++) {//Initialize first row -cnt[i]=0; $              for(intj=0; j<m;j++){ the                   if(mp[i][j]=='H'){ thecnt[i]+= (1<<j); the                   } the               } -           } in            thememset (dp,-1,sizeof(DP)); the            About            for(intI=0; i<top;i++){ thenum[i]=Count_one (Stk[i]); the             if((stk[i]&cnt[0])==0){ thedp[0][i][0]=Num[i]; +             } -           }  the           Bayi          /*for (int i=0;i<top;i++) { the if (stk[i]&cnt[1]) continue; the for (int j=0;j<top;j++) { - if (stk[j]&cnt[0]) continue; - if (stk[i]&stk[j]) continue; the Dp[1][i][j]=max (Dp[1][i][j],dp[0][j][0]+num[i]); the               } the           } the           *///this way, the second line is initialized, not initialized, and not even wrong . -            the            for(intI=1; i<n;i++){ the                for(intt=0; t<top;t++){ the                   if(Stk[t]&cnt[i])Continue;94                    for(intj=0; j<top;j++){ the                       if(Stk[j]&stk[t])Continue; the                        for(intk=0; k<top;k++){ the                           if(Stk[k]&stk[t])Continue;98                           if(dp[i-1][j][k]==-1)Continue; AboutDp[i][t][j]=max (dp[i][t][j],dp[i-1][j][k]+num[t]); -                       }101                   }102               }103           }104           intans=0; the            for(intI=0; i<n;i++){106                for(intj=0; j<top;j++){107                    for(intk=0; k<top;k++){108ans=Max (ans,dp[i][j][k]);109                   } the               }111           } theprintf"%d\n", ans);113     } the     return 0; the}
View Code

POJ 1185 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.