```
백준 11726번 2xn 타일링 C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 11726번 풀이
- 간단한 DP 연습
https://www.acmicpc.net/problem/11726
11726번: 2×n 타일링
2×n 크기의 직사각형을 1×2, 2×1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하시오. 아래 그림은 2×5 크기의 직사각형을 채운 한 가지 방법의 예이다.
www.acmicpc.net
백준 11726번호 2xn타일링은
난이도 쉬움 등급의 문제로서
2 x n 크기의 직사각형을 1 x 2, 2 x 1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하는
문제입니다.
이문제는 3분안에 풀 수 있었는데
이유는 바로 타일채우기 14852번 문제와 매우 유사해서
입력값만 바꿔서 풀었기 떄문입니다.
해당되는 자세한 설명은 아래 링크에 있습니다.
https://codemasterkimc.tistory.com/116
백준 14852번 타일채우기3 C++ 구현해보기
``` 백준 14852번 타일채우기3 C++ 구현해보기 ``` 이번 글을 통해 배워갈 내용 백준 14852 번 풀이 간단한 DP 및 std::array 연습 https://www.acmicpc.net/problem/14852 14852번: 타일 채우기 3 첫째 줄에..
codemasterkimc.tistory.com
30분 정도 위에 링크를 방문하셔서 풀어보시고
안풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.

3가지 경우에 수에 맞춰서 진행을 하였습니다.
14852번과 비슷하기 때문에 자세한 설명은 위에 링크를 참조해주시면 됩니다.
속도보다는 객체 지향적 범용적 프로그램을 구상하다보니 꽁으로 문제를 먹었네요 ㅋㅋㅋ
전체 코드는 다음과 같습니다
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
class CTile
{
private:
std::array<std::array<std::array<long long, 2>, 2>, 1000002> wallMemoization;
int32_t wallWidth;
public:
CTile()
{
wallWidth = 0;
wallMemoization.fill({ {{-1,-1},{-1,-1}} });
}
~CTile()
{
}
void setWallWidth(const int32_t _wallWidth)
{
this->wallWidth = _wallWidth;
}
void printNumberOfCases()
{
std::cout << dp(wallWidth, true, true);
}
long long dp(int32_t width, bool topIsempty, bool bottomIsempty)
{
long long retVal = 0;
const int32_t topIndex = topIsempty ? 1 : 0;
const int32_t bottomIndex = bottomIsempty ? 1 : 0;
const bool isfilled = false;
const bool isEmpty = true;
// 남은 길이 1 이하
if (width <= 1)
{
if (width == 1)
{
// 위아래 모두 빈 경우
if ((topIsempty == true) && (bottomIsempty == true))
{
retVal = 1;
}
// 둘다 찬 경우
else if ((topIsempty == false) && (bottomIsempty == false))
{
retVal = 0;
}
// 위아래 중 하나만 빈 경우
else
{
retVal = 0;
}
}
else if (width == 0)
{
// 위아래 모두 빈 경우
if ((topIsempty == true) && (bottomIsempty == true))
{
retVal = 1;
}
}
else
{
retVal = 0;
}
}
else if (wallMemoization[width][topIndex][bottomIndex] != -1)
{
retVal = wallMemoization[width][topIndex][bottomIndex];
}
else
{
if ((topIsempty == true) && (bottomIsempty == true))
{
// ▣
// ▣
// 2 x 1
retVal += (dp((width - 1), isEmpty, isEmpty));
// ▣▣
// ▣▣
// 1 x 2 - 1 x 2
retVal += (dp((width - 2), isEmpty, isEmpty));
}
else if (((topIsempty == true) && (bottomIsempty == false)) ||
((topIsempty == false) && (bottomIsempty == true)))
{
// ▣
// ▣
// 1 x 1
// ▣
// ▣▣
// 1 x 2
// 1 x 1
retVal += (dp((width - 1), isEmpty, isEmpty));
// 1 x 2
retVal += (dp((width - 1), isEmpty, isfilled));
}
//둘다 찬 경우
else
{
// ▣
// ▣
// 1 x 1
retVal += (dp((width - 1), isEmpty, isEmpty));
}
retVal = retVal % 10007;
wallMemoization[width][topIndex][bottomIndex] = retVal;
}
return retVal;
}
void printWallMemoization()
{
std::cout << "\n";
for (int i = 0; i < this->wallWidth; i++)
{
std::cout << wallMemoization[i][0][0] << " " << wallMemoization[i][0][1] << " " << wallMemoization[i][1][0] << " " << wallMemoization[i][1][1] << "\n";
}
}
};
int main()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
CTile* cTile = new CTile();
int32_t tempVal;
ss >> tempVal;
cTile->setWallWidth(tempVal);
cTile->printNumberOfCases();
//cTile->printWallMemoization();
delete cTile;
}
입력
11
결과
144
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩하시길 바랍니다 ~ :)
참조 및 인용
C++ Primer
Introduction to Algorithms
https://codemasterkimc.tistory.com/35
C++ 이론을 배울수 있는 곳 정리
개요 C++을 배우는 책, 강의, 블로그, 링크 등을 공유합니다. (링크 및 간략한 설명을 하였으나 만약 원작자가 링크를 거는것을 원치 않을 경우 연락주시기 바랍니다.) 서적 https://www.amazon.com/Prime
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/50
300년차 개발자의 좋은 코드 5계명 (Clean Code)
이번 글을 통해 배워갈 내용 좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋
codemasterkimc.tistory.com
'C++ > C++ 알고리즘' 카테고리의 다른 글
| 백준 2718번 4 X N 타일 채우기 C++ 구현해보기 (0) | 2021.09.11 |
|---|---|
| 백준 11727번 2xn 타일링2 C++ 구현해보기 (0) | 2021.09.11 |
| 백준 14852번 타일채우기3 C++ 구현해보기 (0) | 2021.09.11 |
| 백준 15700번 타일채우기4 C++ 구현해보기 (0) | 2021.09.10 |
| 백준 2780번 비밀번호 C++ 구현해보기 (0) | 2021.09.09 |