Gun
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 19690 |
|
Accepted: 7602 |
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 m columns. Each grid of the map may be mountainous (denoted by "H"). It may also be plain (denoted by "P"). For example with. 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 seen in the black area of the attack on the map:
Assuming that an artillery unit is deployed on a plain that is identified in gray on the map, the black grid in the figure represents the area to which it can attack: two grids along the horizontal side. Two panes along 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 troops, in the premise of preventing accidental injury (to ensure that no two artillery units can attack each other, that is, no matter what one of the artillery units in the attack range of other artillery units), in the entire map area, the maximum number of troops to be placed in the Army's artillery units.
Input
The first line consists of two positive integers separated by a space, representing N and M respectively;
The next n rows. Each row contains a continuous m-character (' P ' or ' H '). There are no spaces in the middle. Represents the data for each row in the map sequentially. N <= 100. M <= 10.
Output
Only one row, including an integer k. Indicates the maximum number of artillery units that can be placed.
Sample Input
5 4PHPPPPHHPPPPPHPPPHHP
Sample Output
6
Not easy Ah, debug for an afternoon.
。。
</pre><pre name= "code" class= "CPP" > #include <iostream> #include <stdio.h> #include <math.h > #include <string.h> #include <algorithm> #include <iostream>using namespace std; #define N 105const int Inf=0x3fffffff;char g[n][15];int num[65],cnt[65],cur[n];int top,dp[n][65][65];int getsum (int x) {int t=0; while (x)//get x converted into binary after 1 number {x&= (x-1); t++; } return t;} void Inti (int m) {int i; Top=0; memset (num,-1,sizeof (num)); For (i=0;i< (1<<m); i++) {if (i& (i<<1)) continue; if (i& (i<<2)) continue; Num[top]=i; Cnt[top++]=getsum (i); }}int Main () {int i,j,k,r,n,m; Inti (10); while (scanf ("%d%d", &n,&m)!=-1) {for (i=0;i<n;i++) {scanf ("%s", G[i]); cur[i]=0; for (j=0;j<m;j++) {if (g[i][j]== ' H ') cur[i]|= (1<<J); Get information about the non-placed cannon for each line,} } for (top=0;num[top]!=-1&&num[top]< (1<<m); top++); Because when m=10 is visited, there is an array element memset (Dp,-1,sizeof (DP)) that is not assigned. for (i=0;i<top;i++) {if (num[i]&cur[0]) continue; Dp[0][i][0]=cnt[i]; } for (r=1;r<n;r++)//Enumerate each of the remaining lines {for (i=0;i<top;i++)//each row is desirable for each combination { if (Cur[r]&num[i]) continue; For each combination of the for (j=0;j<top;j++)//Previous line (i-1) {if (num[i]&num[j]) continue; for (k=0;k<top;k++)//enumeration i-2 each combination of rows {if (num[i]&num[k]) continue; if (Num[j]&num[k]) continue; Change to Dp[r-1][j][k]==-1 faster Dp[r][i][j]=max (Dp[r][i][j],dp[r-1][j][k]+cnt[i]); }//When Forward and J, K Row does not conflict}} int ans=0; for (i=0;i<top;i++) { for (j=0;j<top;j++) Ans=max (Ans,dp[n-1][i][j]); } printf ("%d\n", ans); } return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
POJ 1185 Artillery (reduction of national DP)