Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1b805d

Browse files
issue sowon-dev#44 10844
1 parent 4ce6917 commit f1b805d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎src/backjoon/_10844.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/10844
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
public class _10844 {
8+
9+
static Long[][] dp;
10+
static int N;
11+
final static long MOD = 1000000000;
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
N = Integer.parseInt(br.readLine());
16+
dp = new Long[N + 1][10];
17+
18+
// 첫번째 자릿수는 1로 초기화
19+
for(int i = 0; i < 10; i++) {
20+
dp[1][i] = 1L;
21+
}
22+
23+
long result = 0;
24+
25+
// 마지막 자릿수인 1~9까지의 경우의 수를 모두 더해준다.
26+
for(int i = 1; i <= 9; i++) {
27+
result += recur(N, i);
28+
}
29+
System.out.println(result % MOD);
30+
}
31+
32+
static long recur(int digit, int val) {
33+
34+
if(digit == 1) {
35+
return dp[digit][val];
36+
}
37+
38+
// 해당 자리수의 val값에 대해 탐색하지 않았을 경우
39+
if(dp[digit][val] == null) {
40+
// val이 0일경우 다음은 1밖에 못옴
41+
if(val == 0) {
42+
dp[digit][val] = recur(digit - 1 ,1);
43+
}
44+
// val이 1일경우 다음은 8밖에 못옴
45+
else if(val== 9) {
46+
dp[digit][val] = recur(digit - 1, 8);
47+
}
48+
// 그 외의 경우는 val-1과 val+1 값의 경우의 수를 합한 경우의 수가 됨
49+
else {
50+
dp[digit][val] = recur(digit - 1, val - 1) + recur(digit - 1, val + 1);
51+
}
52+
}
53+
return dp[digit][val] % MOD;
54+
}
55+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /