Artillery positions
Time limit:2000ms Memory limit:65536k
Total submissions:23655 accepted:9148
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 4
PHPP
Pphh
Pppp
PHPP
Phhp
Sample Output
6
Source
Noi 01
Topic link : http://poj.org/problem?id=1185;
idea : m<=10--"pressure DP, pre-processing the first to meet the non-overlapping (common per row) of the number of scenarios, and then row by line enumeration and the conditions of the row;
The state of the bank is related to the first two lines, so the DP is a three-dimensional dp[i][now][pre], transferred from the state Dp[i-1][pre][prepre];
Code ://pojwa code, other Ojac ....
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#define MAXN 101#define MAXM#define NUMusing namespace STD;intDp[maxn][num][num];intN,m;CharS[MAXM];intCAN[MAXN];intSta[num];intSum[num];intans=0;intTotintCalintx) {intCnt=0; while(x) {x&= X-1); cnt++; }returnCNT;}voidInit () {tot=0; for(intI=0;i< (1<<M); i++) {if(! (i& (i>>1)) &&! (i& (i>>2)) {sta[tot]=i; Sum[tot]=cal (i); tot++; } }}intMain () { while(scanf("%d%d", &n,&m)!=eof) {ans=0; for(intI=0; i<n;i++) {scanf('%s ', s); can[i]=0; for(intj=0; j<m;j++) {if(s[j]==' P ') can[i]+=1<< (m-j-1); }} init ();memset(dp,-1,sizeof(DP)); for(intI=0; i<tot;i++) {if(! ((~can[0]) (&sta[i])) {dp[0][i][0]=sum[i]; } } for(intI=1; i<n;i++) { for(intj=0; j<tot;j++) {if((~can[i]) &sta[j])Continue; for(intk=0; k<tot;k++) {if((~can[i-1]) &sta[k])Continue;if(Sta[k]&sta[j])Continue; for(intp=0;p <tot;p++) {if(Sta[p]&sta[j])Continue;if(dp[i-1][k][p]==-1)Continue; Dp[i][j][k]=max (dp[i][j][k],dp[i-1][K][P]+SUM[J]); }if(i==n-1) Ans=max (Ans,dp[i][j][k]);//cout<<i<< "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;} } }printf("%d\n", ans); }}
[POJ 1185] [Codevs 1647] [NOI 2001] Artillery position-like pressure DP