1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include<stdio.h> #include<string.h> #define mmset(a,b) memset(a,b,sizeof(a)) const int INF = 1 << 12 + 10; long long dp[13][2100]; int n,m;
void dfs(int i,int j,int state,int next); int main() { while(~scanf("%d%d",&n,&m) && (n+m)) { if(n > m) { n = n ^ m; m = n ^ m; n = n ^ m; } mmset(dp,0); dp[1][0] = 1; for(int j = 1; j <= m; j++) { for(int state = 0; state < (1 << n); state++) { if(dp[j][state] > 0) { dfs(0,j,state,0); } } } printf("%lld\n",dp[m+1][0]); } return 0; } void dfs(int i,int j,int state, int next) { if(i == n) {
dp[j+1][next] += dp[j][state]; return; } else { if((state&(1 << i) )== 0) { dfs(i+1,j,state,next | (1<< i)); }
if(i + 1 < n &&(state & (1 << i)) == 0 && (state & (1 << (i+1))) == 0) { dfs(i+2,j,state,next); } if((state & (1 << i)) > 0) { dfs(i+1,j,state,next); } } }
|