Today I started srm, and PT thought of dp but I couldn't help it. But after reading rng_58's God code, I felt like a sea of Sky (staring at one afternoon, I can't bear to look straight,
TC is really a good place... Like!
In fact, it is to write the ordinary brick-laying problem in a way similar to the plug dp by Lattice recurrence. The following code should be understandable.
#include <cstdio> #include <cstring> #include <algorithm> long long dp[2][1<<11]; int main() { int n , m; while(scanf("%d%d",&n,&m),n||m) { int cur = 0 , nxt = 1; dp[cur][0] = 1; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { memset(dp[nxt],0,sizeof(dp[nxt])); for(int s = 0; s < (1<<m); s++) if(dp[cur][s]){ if(s&1){dp[nxt][s>>1] += dp[cur][s];continue;} if(j+1<m && !(s&2) ){ int mask = ( s | 2 ) >> 1; dp[nxt][mask] += dp[cur][s]; } if(i+1<n) { int mask = (s | (1<<m)) >> 1; dp[nxt][mask] += dp[cur][s]; } } std::swap(cur,nxt); } } printf("%I64d\n",dp[cur][0]); } return 0; } #include <cstdio>#include <cstring>#include <algorithm>long long dp[2][1<<11];int main() { int n , m; while(scanf("%d%d",&n,&m),n||m) { int cur = 0 , nxt = 1; dp[cur][0] = 1; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { memset(dp[nxt],0,sizeof(dp[nxt])); for(int s = 0; s < (1<<m); s++) if(dp[cur][s]){ if(s&1){dp[nxt][s>>1] += dp[cur][s];continue;} if(j+1<m && !(s&2) ){ int mask = ( s | 2 ) >> 1; dp[nxt][mask] += dp[cur][s]; } if(i+1<n) { int mask = (s | (1<<m)) >> 1; dp[nxt][mask] += dp[cur][s]; } } std::swap(cur,nxt); } } printf("%I64d\n",dp[cur][0]); } return 0;}
The following is the example of a year ago...
#include<stdio.h> #include<string.h> int h,w; __int64 dp[15][2050]; void dfs1(int row,int state,int col,int state2){ if(col>w) return ; if(col==w) { dp[row+1][state2]+=dp[row][state]; return ; } if(!(state&(1<<col))) dfs1(row,state,col+1,state2+(1<<col)); else dfs1(row,state,col+1,state2); } void dfs2(int row,int state,int col,int state2){ if(col>w) return ; if(col==w) { if(state2!=state) dp[row][state2]+=dp[row][state]; return ; } if(!(state&(1<<col)) && !(state&(1<<(col+1)))) dfs2(row,state,col+2,state2+(1<<col) + (1<<(col+1))); dfs2(row,state,col+1,state2); } void gao(){ int i,j; dp[0][(1<<w)-1]=1; for(i=0;i