POJ 1185 Artillery position pressure dp//The problem is similar to the previous pieces, but this is a change from the previous//piece, this condition let us have to consider the state before this state, that is, in//a state record another state, said may be a bit around//so to speak: dp[ I][S][V] indicates that the first line of the state is S (still binary number//expression, such as 5 is 101, the first column and the third row of artillery), the I-1 Line//is the state of V (also binary digital representation) when the maximum number of guns//soldiers can be placed, the state transition is://DP I S [V] = max (Dp[i][s][v],dp[i][v][k]+sum[s]),//Sum[s] represents the number of artillery in the S state (i.e. 1 of them),//The Last of all S, and V's dp[0][s][v] Max is the answer we ask// This problem has a lot of details to deal with, such as all in a row of legitimate status in advance//to find out, so that the enumeration state can be directly judged those who are legitimate//played the role of pruning, the second is the boundary problem, for the general situation//dp[i][s][v] = 1, In addition we have to put the initial value of each legal state to put//in, dp[0][(legal State)][0]= the number of legitimate artillery. Daniel, the idea is really Daniel, worship unceasingly//Actually this question I really can not write, see the problem solving report of Daniel have looked at two or three/hour, then really understand, also own knock once WA no number of hair, finally//real AC. Keep practicing, as a valuable experience of your own. This problem, there are a lot of places to be fastidious, continue to practice it ... #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat > #include <climits> #include <cmath> #include <complex> #include <cstdio> #include < cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set># Include <stack> #include <vector> #define CEIL (A, B) (((a) + (b)-1) #define Endl ' \ n ' #define GCD __gcd# Define Highbit (x) (1ull<< (63-__builtin_clzll (x))) #define Popcount __builtin_popcountlltypedef Long Long ll; using namespace Std;const int MOD = 1000000007;const long Double PI = ACOs ( -1.L); Template<class t> inline T LCM (cons T t& A, const t& b) {return A/GCD (A, b) *b;} Template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} int dp[105][70][70];int state[70];int sum[70];int mp[105];char str[15];bool ok (int x) {if (x& (x>>1)) return False;if (x& (x>>2)) return False;return true; int get (int x) {int i=0;while (x) {if (x&1) i++;x> >=1;} return i;} int N,m;int num;void Find () {num=0;for (int i=0;i< (1<<m); i++) {if (ok (i)) {State[num] = I;sum[num] = get (i); num++;} }}void init () {memset (Mp,0,sizeof (MP)), for (int i=0;i<n;i++) {scanf ('%s ', str), for (int j=0;str[j]!=0;j++) if (Str[j] = = ' H ') mp[i] = Mp[i] | (1<<J);}} void Solve () {find (), memset (Dp,-1,sizeof (DP)), for (int i=0;i<num;i++) if (! ( State[i]&mp[0]) dp[0][i][0] = sum[i];for (int i=1;i<n;i++) {for (int j=0;j<num;j++) {if (Mp[i]&state[j]) continue;for (int k=0;k<num;k++) {if (State[j]&state[k]) continue;for (int l=0;l<num;l++) {if (state[j]& State[l]) continue;if (State[k]&state[l]) continue;if (dp[i-1][k][l]==-1) continue;dp[i][j][k] = max (dp[i-1][k][l ]+sum[j],dp[i][j][k]);}}} int ans=0;for (int i=0;i<num;i++) {for (int j=0;j<num;j++) {//if (state[i]&state[j]) Continue;ans = max (ans,dp[ N-1][I][J]);}} printf ("%d\n", ans);} int main () {//find (); Freopen ("G:\\code\\1.txt", "R", stdin), while (scanf ("%d%d", &n,&m)!=eof) {iniT (); Solve ();} return 0;}
POJ 1185 Artillery position-like pressure DP