HDU 2046 Dominoes paved squares

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=2046

Domino Shop Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 35779 Accepted S Ubmission (s): 17390
problem Description
In a rectangular square of 2xn, fill the squares with a 1x2 domino, enter N, and output the total number of paving schemes.

For example, when n=3, for 2x3 squares, the Domino placement scheme has three kinds, such as:


Input
The input data consists of multiple lines, each containing an integer n, indicating that the size of the rectangular square of the test instance is 2xn (0<n<=50).

Output
For each test instance, output the total number of placement scenarios, one row for each instance output.

Sample Input
1
3
2

Sample Output
1
3

2


Author

Lcy


Source

Recursive Solution topic exercise (for beginner)

Recommend
Lcy

The rectangular squares of--2*n are covered with 1*2 dominoes. Q: Given an n, the total number of scenarios for the solution placement.

Idea--apparently f (1) =1,f (2) =2,f (3) = 3, now we examine the last column: If the last column is vertical, then the number of schemes is F (n-1), and if the last column is placed sideways, it must be two horizontally, at which point the number of schemes is f (n-2). Therefore, f (n) =f ( n-1) +f (n-2), n>2, is the number of Fibonacci. And because n maximum is 50, so the direct recursion can.

Complexity analysis--time complexity: O (n), spatial complexity: O (n)

Attach the AC code:


#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm>using namespace std;typedef unsigned int ui;typedef long long ll;typedef unsigned long long ull;typedef long Double Ld;const double PI = 3.14159265;const double E = 2.71828182846;int Main () {Ios::sync_with_stdio (false); LL num[51] = {1, 1, 2};for (int i=3; i<51; i++) num[i] = Num[i-1]+num[i-2];int n;while (cin >> N) {cout << nu M[n] << Endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2046 Dominoes paved squares

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.