Table of Contents
Question content
Solution
Home Backend Development Golang How to calculate how many possible combinations of 1x1, 1x2, 2x1 tiles can fill a 2 x N floor?

How to calculate how many possible combinations of 1x1, 1x2, 2x1 tiles can fill a 2 x N floor?

Feb 10, 2024 am 08:54 AM

如何计算 1x1、1x2、2x1 瓷砖的可能组合有多少种可以填充 2 x N 地板?

php Editor Shinichi will answer an interesting and brain-burning question for everyone: How to calculate how many possible combinations of 1x1, 1x2, and 2x1 tiles can fill a 2 x N floor? This problem involves the knowledge of combinatorial mathematics and dynamic programming. Through analysis and derivation, we can come up with a simple and effective calculation method. Next, let’s explore the answer to this question together!

Question content

I just did a technical test and am confused about the task. My goal is to understand how to solve this "covered floor" problem. I honestly don't know where to start.

The task is:

  1. There are 2 x n layers.
  2. We have 1x1, 1x2, 2x1 tiles to fill the floor.

The issue is:

  1. solution(1) Expected output is 2, actual output is 2.
  2. However, solution(2) expected output is 7, actual output is 3.

The current solution is:

  1. 1x1 can always fill 2 x n layers, so the possible ways start at 1.
  2. If the remaining floors mod 2 is 0, the possible ways are increased by 1.

The problem with the current solution is that it doesn't differentiate between 1x2 and 2x1 blocks. So for solution(2) the actual output is 3 instead of 7.

Code

package main

import "fmt"

// Solution is your solution code.
func Solution(n int) int {
    possibleWays := 1

    floorArea := 2 * n
    // Your code starts here.

    for i := floorArea - 1; i >= 0; i-- {
        residualFloorArea := floorArea - i
        fmt.Println(i, residualFloorArea)
        if residualFloorArea%2 == 0 {
            fmt.Println("punch")
            possibleWays += 1
        }
    }

    return possibleWays
}

func main() {
    fmt.Println(Solution(1))
    fmt.Println("next")
    fmt.Println(Solution(2))
}
Copy after login

Solution

A more descriptive and thorough attempt:

The number of methods called to cover the 2xn grid is x_n, the number of methods covering the 2xn 1 grid is y_n, and the number of methods covering the 2xn 2 grid is z_n.

Basic case:

  • x_0 = 1, y_0 = 1, z_0 = 2
  • x_1 = 2, y_1 = 3, z_1 = 5

Induction steps, n >=2:

-- --
      |  |  |
 -- -- -- --  ...
|xx|  |  |  |
 -- -- -- --
Copy after login

Consider the leftmost cell of a 2xn 2 grid, if it is covered by a 1x1 tile, then the rest is a 2xn 1 grid, otherwise, it is covered by a 1x2 tile, and the rest is a 2xn grid. therefore,

z_n = x_n y_n

-- --
   |  |  |
 -- -- --  ...
|xx|  |  |
 -- -- --
Copy after login

Consider the leftmost cell of a 2xn 1 grid, if it is covered by a 1x1 tile, the remainder will be 2xn grid, otherwise, it is covered by a 1x2 tile, the remainder will be 2x(n- 1) 1 grid. therefore,

y_n = x_n y_(n-1)

-- --
|xx|  |
 -- --  ...
|  |  |
 -- --
Copy after login

Consider the upper left corner of a 2xn grid, if it is covered by a 1x1 tile, the remainder will be 2x(n-1) 1 grid, if it is covered by a 1x2 tile, the remainder will be A 2x(n-2) 2 grid, otherwise, it is covered by 2x1 tiles and the remainder will be a 2x(n-1) grid. therefore:

x_n = y_(n-1) z_(n-2) x_(n-1)

Replacing z_n with x_n y_n, we have:

  • x_n = x_(n-1) x_(n-2) y_(n-1) y_(n-2)
  • y_n = x_n y_(n-1)

Now, just iterate over each value:

package main

import "fmt"

// Solution is your solution code.
func Solution(n int) int {
    if n == 0 {
        return 1
    } else if n == 1 {
        return 2
    }

    x := make([]int, n + 1)
    y := make([]int, n + 1)
    x[0] = 1
    y[0] = 1
    x[1] = 2
    y[1] = 3

    for i := 2; i <= n; i++ {
        x[i] = x[i - 1] + x[i - 2] + y[i - 1] + y[i - 2]
        y[i] = x[i] + y[i - 1]
    }

    return x[n]
}

func main() {
    fmt.Println(Solution(1))
    fmt.Println("next")
    fmt.Println(Solution(2))
}
Copy after login

You can do this without using slices, but it's easier to understand. Playground Demonstration

The above is the detailed content of How to calculate how many possible combinations of 1x1, 1x2, 2x1 tiles can fill a 2 x N floor?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang and C  : Concurrency vs. Raw Speed Golang and C : Concurrency vs. Raw Speed Apr 21, 2025 am 12:16 AM

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Golang's Impact: Speed, Efficiency, and Simplicity Golang's Impact: Speed, Efficiency, and Simplicity Apr 14, 2025 am 12:11 AM

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang vs. Python: Key Differences and Similarities Golang vs. Python: Key Differences and Similarities Apr 17, 2025 am 12:15 AM

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

Golang vs. C  : Performance and Speed Comparison Golang vs. C : Performance and Speed Comparison Apr 21, 2025 am 12:13 AM

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

Golang and C  : The Trade-offs in Performance Golang and C : The Trade-offs in Performance Apr 17, 2025 am 12:18 AM

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

C   and Golang: When Performance is Crucial C and Golang: When Performance is Crucial Apr 13, 2025 am 12:11 AM

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

The Performance Race: Golang vs. C The Performance Race: Golang vs. C Apr 16, 2025 am 12:07 AM

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

See all articles