Poj 1185 Artillery Position (State compression DP)

Source: Internet
Author: User
Artillery positions
Time limit:2000 ms   Memory limit:65536 K
Total submissions:14519   Accepted:5406

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 4 phpppphhppppphppphhp

Sample output

 
6

Source

The Noi 01 status compresses DP. The placement of the current row is only related to the first two rows. Pre-process the valid status of a row. DP [I] [J] [k] indicates row I. The last two statuses are J and K, and the maximum values can be placed. Use a scrolling array,
 //  ========================================================== ==========================================  //  Name: poj. cpp  //  Author: //  Version:  //  Copyright: Your copyright notice  //  Description: Hello world in C ++, ANSI-style  //  ========================================================== ==========================================  # Include <Iostream> # Include <Stdio. h> # Include < String . H> # Include <Algorithm> Using   Namespace  STD;  Int State [ 70  ];  Int Num [ 70  ];  Int  CNT;  Int DP [ 2 ] [ 70 ] [ 70  ]; Int Calc ( Int X) //  Calculate the number of 1 in the binary of X.  {  Int Ret = 0  ;  While  (X ){  If (X & 1 ) RET ++ ; X >>= 1  ;} Return  RET ;}  Void  Init () {CNT = 0  ;  For ( Int I = 0 ; I <( 1 < 10 ); I ++ )  If (I & (I < 1 ) = 0 & (I < 2 ) = 0 & (I & (I> 1 ) = 0 & (I & (I> 2 ) = 0  ) {Num [CNT] = Calc (I); State [CNT ++] = I ;}}  Int A [ 110  ];  Char STR [ 110 ] [ 20  ];  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );  Init ();  Int  N, m;  While (Scanf ( "  % D " , & N, & M) = 2  ){  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % S  " ,& STR [I]); A [I] = 0  ;  For (Int J = 0 ; J <m; j ++ ) {A [I] <= 1  ;  If (STR [I] [J] = '  H  ' ) A [I] | = 1  ;} Memset (DP, - 1 , Sizeof  (DP )); Int Now = 0  ; DP [now] [  0 ] [ 0 ] = 0  ;  Int TOT = ( 1 < M );  For ( Int I = 0 ; I <n; I ++ ){ For ( Int J = 0 ; J <CNT & State [J] <tot; j ++ )  For ( Int K = 0 ; K <CNT & State [k] <tot; k ++ )  If (DP [now] [J] [k]! =- 1  ){  For ( Int X = 0 ; X <CNT & State [x] <tot; X ++ )  If (State [x] & A [I]) = 0 & (State [x] & State [J]) = 0 & (State [x] & State [k]) = 0  ) DP [now ^ 1 ] [K] [x] = max (DP [now ^ 1 ] [K] [X], DP [now] [J] [k] + Num [x]);} now ^ = 1 ;}  Int Ans = 0  ;  For ( Int I = 0 ; I <CNT & State [I] <tot; I ++ )  For ( Int J = 0 ; J <CNT & State [J] <tot; j ++ ) Ans = Max (ANS, DP [now] [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.