2023. 8. 26. 18:27γCoding Test/BOJ
πλ¬Έμ 보λ¬κ°κΈ°
π§π»π»νμ΄ κ³Όμ
Nμ리 μ«μμμ μΌμ μλ¦¬κ° 0 ~ 9λ‘ λλλ κ³λ¨ μλ€μ κ°μλ₯Ό μμλ΄ μλ€. λ¨Όμ , \( N = 1\)μΌ λλ λ€μκ³Ό κ°μ΅λλ€.
[ N = 1 μΌ λ ]
i = 0 | i = 1 | i = 2 | i = 3 | i = 4 | i = 5 | i = 6 | i = 7 | i = 8 | i = 9 |
0 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
[ N = 2 μΌ λ ]
i = 0 | i = 1 | i = 2 | i = 3 | i = 4 | i = 5 | i = 6 | i = 7 | i = 8 | i = 9 |
1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 1 |
[ N = 3 μΌ λ ]
i = 0 | i = 1 | i = 2 | i = 3 | i = 4 | i = 5 | i = 6 | i = 7 | i = 8 | i = 9 |
1 | 3 | 3 | 4 | 4 | 4 | 4 | 4 | 3 | 2 |
μ΄ μ λκΉμ§ μ§νν΄λ³΄λ, κ·μΉμ±μ΄ 보μ΄κΈ° μμν©λλ€.
N μ리 μ«μμμ i μ«μλ‘ λλλ κ³λ¨ μμ κ°μλ₯Ό μ μ₯νλ λ°°μ΄μ dp[N][i]λΌκ³ νλ€λ©΄, κ·μΉμ±μ λ€μκ³Ό κ°μ΅λλ€.
dp[N][0] = dp[N-1][1]
dp[N][i] = dp[N-1][i-1] + dp[N-1][i+1]; (i > 0 and i < 9)
dp[N][9] = dp[N-1][8]
μ¦, λμ νλ‘κ·Έλλ°μ μ΄μ©νμ¬ N = 1μΌ λμ iκ°λ€μ λ°νμΌλ‘ N = 2, N = 3μ κ°λ€μ μ°¨λ‘λλ‘ μμλΌ μ μμ΅λλ€.
λν, λμ νλ‘κ·Έλλ°μ μ¬μ©ν λ°°μ΄μ \( 2 \times 10 \) ν¬κΈ°λ§ κ°μ§λ€λ©΄, λͺ¨λ Nμ λν κ³λ¨ μ κ°μλ₯Ό μμλΌ μ μμ΅λλ€.
μ΄κ²μ΄ μ΄λ»κ² κ°λ₯ν κΉμ? μ λ λ€μκ³Ό κ°μ λ°©λ²μΌλ‘ ꡬνν μ μμμ΅λλ€.
λ¨Όμ , νμ κ°λ¦¬ν€λ λ³μλ₯Ό λ€μκ³Ό κ°μ΄ 2κ° μ μΈν©λλ€.
int writeBufferIndex = 0, readBufferIndex = 1;
- writeBufferIndexλ readBufferIndexμ νμμ μ½μ΄μ μλ‘ κ°μ μ°κ² λλ νμ μλ―Έν©λλ€.
- μμμ λ§λ€μλ μ νμμ λΉμ νμλ©΄, writeBufferIndexλ N, readBufferIndexλ N - 1μ ν΄λΉνλ€κ³ λ³Ό μ μμ΅λλ€.
- ν νμ λνμ¬ λͺ¨λ μ°μ°μ΄ λλ¬λ€λ©΄, writeBufferIndexμ readBufferIndexκ° μλ‘ κ°μ κ΅νν¨μΌλ‘μ¨, μν μ λ°κΏμ λ€μ μ°μ°μ μ΄μ΄λκ°κ² λ©λλ€.
Nμ΄ ν΄μλ‘ 2μ°¨μ λ°°μ΄μ ν¬κΈ°κ° 컀μ§κ² λμ΄ λ©λͺ¨λ¦¬λ₯Ό λ§μ΄ μ°κ² λ©λλ€. μ λ μ μ νμμ λμΆνλ κ³Όμ μμ νμ΄ Nκ°κΉμ§ νμ μμ κ² κ°λ€λ μκ°μ΄ λ€μκ³ , κ·Έλν½μ€ λ λλ§μμ μ¬μ©λλ λλΈ λ²νΌλ§(Double Buffering) κΈ°λ²μμ μμ΄λμ΄λ₯Ό μ»μ΄, μμ κ°μ΄ μκ³ λ¦¬μ¦μ ꡬμ±νκ² λμμ΅λλ€.
βοΈμμ€ μ½λ λ° κ²°κ³Ό
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000000;
const int DIGIT_COUNT = 10;
const int BUFFER_COUNT = 2;
void Swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(NULL); cin.tie(NULL);
int N;
cin >> N;
// 1. μ΄κΈ°ν
vector<vector<int>> dp(BUFFER_COUNT, vector<int>(DIGIT_COUNT));
int writeBufferIndex = 0, readBufferIndex = 1;
int sum = 9;
for (int i = 1; i < DIGIT_COUNT; i++)
dp[writeBufferIndex][i] = 1;
// ------------------------------------
// 2. λ‘μ§
Swap(writeBufferIndex, readBufferIndex);
for (int i = 1; i < N; i++)
{
sum = 0;
for (int j = 0; j < DIGIT_COUNT; j++)
{
if (j == 0 or j == 9)
{
int targetIndex = (j == 0) ? j + 1 : j - 1;
int newValue = dp[readBufferIndex][targetIndex];
dp[writeBufferIndex][j] = newValue;
sum = (sum + newValue) % MOD;
continue;
}
dp[writeBufferIndex][j] = (dp[readBufferIndex][j - 1] + dp[readBufferIndex][j + 1]) % MOD;
sum = (sum + dp[writeBufferIndex][j]) % MOD;
}
Swap(writeBufferIndex, readBufferIndex);
}
cout << sum;
return 0;
}
'Coding Test > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 16139λ² | μΈκ°-μ»΄ν¨ν° μνΈμμ© (C++) (1) | 2023.08.27 |
---|---|
[BOJ] 11659λ² | κ΅¬κ° ν© κ΅¬νκΈ° 4 (C++) (0) | 2023.08.27 |
[BOJ] 3190λ² | λ± (C++) (1) | 2023.08.26 |
[BOJ] 16198λ² | μλμ§ λͺ¨μΌκΈ° (C++) (0) | 2023.08.25 |
[BOJ] 1759λ² | μνΈ λ§λ€κΈ° (C++) (0) | 2023.08.25 |