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 4PHPPPPHHPPPPPHPPPHHP
Sample output
6
Only artillery troops can be placed on the ground, and the attack range can be switched to avoid accidental injuries. The maximum number of artillery troops can be placed.
Idea: there should be no artillery troops with a distance of 1 or 2, DP [I] [J] [k] indicates the maximum number of artillery troops in the K State of row I in the J state of row I-1.
AC code:
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <cstring>#include <stdlib.h>using namespace std;int n,m,k;int line[105];int state[100];int dp[105][100][100];int v[105];bool ok(int x){ if(x&(x<<1)) return false; if(x&(x<<2)) return false; return true;}int get1(int x){ int cnt=0; while(x){ cnt++; x&=(x-1); } return cnt;}int main(){ scanf("%d%d",&m,&n); for(int i=0;i<m;i++){ char s[15]; scanf("%s",&s); for(int j=0;j<n;j++){ if(s[j]=='H') line[i]=line[i]|(1<<j); } } memset(state,0,sizeof(state)); k=0; for(int i=0;i<(1<<n);i++){ if(ok(i)) state[k++]=i; } memset(dp,-1,sizeof(dp)); for(int i=0;i<k;i++){ v[i]=get1(state[i]); if(!(state[i]&line[0])) dp[0][0][i]=v[i]; } for(int i=1;i<m;i++){ for(int j=0;j<k;j++){ if(line[i]&state[j]) continue; for(int l=0;l<k;l++){ if(state[j]&state[l]) continue; for(int x=0;x<k;x++){ if(state[j]&state[x]) continue; if(dp[i-1][l][x]==-1) continue; dp[i][x][j]=max(dp[i][x][j],dp[i-1][l][x]+v[j]); } } } } int ans=0; for(int i=0;i<m;i++){ for(int j=0;j<k;j++){ for(int l=0;l<k;l++){ //printf("%d",dp[i][j][l]); ans=max(ans,dp[i][j][l]); } } } printf("%d\n",ans); return 0;}
Zookeeper