작성일 :

문제 링크

30162번 - Filling Shapes

설명

주어진 모양으로 3 × n 보드를 빈칸 없이 채우는 방법의 수를 구하는 문제입니다.


접근법

이 도형으로 3 × n 보드를 채우려면, 열을 항상 두 칸씩 묶어서 생각하게 됩니다.

따라서 n이 홀수이면 애초에 채울 수 없어서 답은 0입니다. 반대로 n이 짝수라면, 길이 2를 하나 늘릴 때마다 이전 경우마다 선택이 두 가지씩 생기므로 전체 경우의 수는 2^(n/2)가 됩니다.

즉 정답은 홀수일 때 0, 짝수일 때 2^(n/2)입니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;

class Program {
  static void Main() {
    int n = int.Parse(Console.ReadLine()!);

    if (n % 2 == 1) {
      Console.WriteLine(0);
      return;
    }

    Console.WriteLine(1L << (n / 2));
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  if (n % 2 == 1) {
    cout << 0 << "\n";
    return 0;
  }

  cout << (1LL << (n / 2)) << "\n";

  return 0;
}

Tags: 30162, BOJ, C#, C++, DP, 백준, 알고리즘

Categories: ,