標籤:des style blog http io ar color os sp
給出一個n*m的棋盤,及一個小的矩形1*2,問用這個小的矩形將這個大的棋盤覆蓋有多少種方法。
dp[i][j]:有多少種方法,可以使得第i行的狀態為j
dp[i][j]=sum{dp[i-1][k],k可以通過合法變化變成狀態j}
0:該位置空餘
1:該位置被占
有的人問,每個位置不是3種狀態嗎?即不放矩形,橫放一個矩形,豎放一個矩形
當然了,這樣定義狀態也是可以的,用三進位。
用三進位的話,位元運算什麼的都要手寫比較麻煩……
還是二進位吧,那為何這樣定義是可以的呢?
假設在某位置橫放是以這個位置為矩形的左端,豎放是以這個位置為矩形的下端
那麼,如果i行j列被佔了,那麼第i+1行j列是不是不能豎放?
如果i行j列沒有被佔了,那麼第i+1行j列是不是只能豎放?
這樣考慮,問題就可以由放矩形變成佔據一個位置
如果還是不明白的,看看My Code就明白了,如果還是不明白……
#include<iostream>#include<cstring>using namespace std;long long dp[20][1<<11];int n,m;void dfs(int s,int row,int line,long long val){int i,k;k=m-1;dp[row][line]+=val;for(i=s;i<k;i++)if((line>>i&1)==0)if((line>>i+1&1)==0)dfs(i+2,row,line|(1<<i)|(1<<i+1),val);}int main(){int i,j,k;while(cin>>n>>m){if(n==0&&m==0)break;memset(dp,0,sizeof(dp));dfs(0,0,0,1);k=1<<m;for(i=1;i<n;i++)for(j=0;j<k;j++)dfs(0,i,k-1^j,dp[i-1][j]);cout<<dp[n-1][k-1]<<endl;}return 0;}
POJ - 2411
Mondriaan‘s Dream
| Time Limit:3000MS |
|
Memory Limit:65536KB |
|
64bit IO Format:%I64d & %I64u |
SubmitStatus
Description
Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his ‘toilet series‘ (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.
Expert as he was in this material, he saw at a glance that he‘ll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won‘t turn into a nightmare!
Input
The input contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.
Output
For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.
Sample Input
1 21 31 42 22 32 42 114 110 0
Sample Output
10123514451205
Source
Ulm Local 2000
poj 2411 Mondriaan's Dream