Originally is to see this topic is the courseware recommended topic among the highest AC rate, the results also looked at two days to see understand t^t, three-state compression, what the devil, a DP Incredibly also 100 lines of code, too outrageous = =
Reference blog: http://www.cnblogs.com/vongang/archive/2012/07/30/2615322.html http://www.cnblogs.com/yefeng1627/archive/ 2013/01/15/2861786.html
The code is the former, according to the back, the main idea is to use the three-way expression of the current state and then the recursive method to find the optimal solution, but also with the scroll array two only to represent the current row and the state of the previous row. The core is the choice of 2x3 and 3x2, two rows and three columns why only judge one on the line, because the state of the scrolling array is that the current state is the state of the previous row, so the value of the current row can be judged. Specific look at the code
/*********** poj1038 2016.1.5 632K 1016MS C + + 2427B others code ***********/#include <stdio.h> #include <string.h> #
Include<stdlib.h> #define MAX (A, B) (A>B?A:B) #define N 59050 #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX (A, B) (A>B?A:B) #define N 59050//tri-binary int tri[12]={
0,1,3,9,27,81,243,729,2187,6561,19683,59049};
int map[151][11];
int dp[2][n];
Previous state, current state int info_pre[11],info_cur[11];
int n,m,pass;
initialize void Init () {memset (map,0,sizeof (map));
Memset (Dp,-1,sizeof (DP));
}//Convert decimal int switch_ten (int *p) {int i,ans;
for (i=1,ans=0;i<=m;i++) {ans + = p[i]*tri[i];
} return ans;
}//Convert the three-input void Switch_tri (int t,int *p) {int i;
for (i=1;i<=m;i++) {p[i] = t%3;
t/=3;
} return;
} void Dfs (int i,int j,int cnt,int stata) {int k;
Dp[i%2][stata] = Max (dp[i%2][stata],cnt);
if (J >= m) return; if (!info_pre[j] &&!info_pre[j+1] &&!iNFO_CUR[J] &&!info_cur[j+1]) {//3*2 arrangement info_cur[j] = info_cur[j+1] = 2;
K = Switch_ten (info_cur);
DFS (I,J+2,CNT+1,K);
info_cur[j]=info_cur[j+1]=0;
} if (J < m-1 &&!info_cur[j] &&!info_cur[j+1] &&!info_cur[j+2]) {//2*3 arrangement
INFO_CUR[J] = info_cur[j+1] = info_cur[j+2] = 2;
K = Switch_ten (info_cur);
DFS (I,J+3,CNT+1,K);
INFO_CUR[J] = info_cur[j+1] = info_cur[j+2] = 0;
} dfs (I,j+1,cnt,stata);
return;
} int main () {int t,k,i,j,l,stata,ans;
scanf ("%d", &t);
while (t--) {Init ();
scanf ("%d%d%d", &n,&m,&k);
while (k--) {scanf ("%d%d", &i,&j);
MAP[I][J] = 1;
} for (i=1;i<=m;i++) {info_pre[i] = Map[1][i] + 1;
} Stata = Switch_ten (Info_pre);
Dp[1][stata] = 0; for (i=2;i<=n;i++) {for (j=0;j<tri[m+1];j++) dP[I%2][J] =-1;
for (j=0;j<tri[m+1];j++) {if (dp[(i+1)%2][j] = =-1) continue;
Switch_tri (J,info_pre);
for (l=1;l<=m;l++) {if (Map[i][l]) info_cur[l] = 2;
else info_cur[l] = Max (info_pre[l]-1,0);//Is the state of the previous row to take 1 or 0} pass = dp[(i+1)%2][j];
DFS (I,1,pass,switch_ten (info_cur));
}} for (i=0,ans=0;i<tri[m+1];i++) {ans = Max (ans,dp[n%2][i]);
} printf ("%d\n", ans);
} return 0;
}