Sicily 1527. tiling a grid with dominoes

Source: Internet
Author: User
Tags creative commons attribution

There is a 4xn Board, an unlimited 1X2 Domino card. Enter n to calculate the number of methods that cover the Board perfectly.

The method is as follows:

The two-dimensional array DP [N] [m] indicates the number of arrangement methods when n columns exist and the column status is M. M is represented by four bits in binary format. The a bit indicates the status of row A of the column. 1 indicates that the column is overwritten, and 0 indicates that the column is not overwritten. The initial status is:

DP [1] [3] = DP [1] [6] = DP [1] [12] = DP [1] [15] = DP [1] [0] = 1;

Status transfer:

DP [T] [15] = DP [T-1] [15] + dp [T-1] [12] + dp [T-1] [3] + dp [T-1] [6] + DP [T-1] [0];
DP [T] [9] = DP [T-1] [6];
DP [T] [12] = DP [T-1] [15] + dp [T-1] [3];
DP [T] [3] = DP [T-1] [15] + dp [T-1] [12];
DP [T] [6] = DP [T-1] [15] + dp [T-1] [9];
DP [T] [0] = DP [T-1] [15];

Code:

// Problem#: 8223// Submission#: 2127308// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>#include <algorithm>#include <cstring>using namespace std ; int main(){    int d[31][16] ;    memset(d,0,sizeof(d)) ;     d[1][15] =  d[1][12] = d[1][6] =  d[1][3] =d[1][0]= 1 ;     for(int i = 2 ; i <= 30 ; i ++)    {        d[i][15] = d[i-1][0]+d[i-1][3]+d[i-1][6]+d[i-1][12]+d[i-1][15] ;        d[i][12] = d[i-1][3] + d[i-1][15] ;        d[i][9] = d[i-1][6] ;        d[i][6] = d[i-1][15] + d[i-1][9] ;        d[i][3] = d[i-1][15] + d[i-1][12] ;        d[i][0] = d[i-1][15] ;    }    int k = 1 ;     int n ;cin >> n ;    while(n--)    {        int c ;        cin >> c ;        cout << k++ <<" " << d[c][15] << endl;     }}                                 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.