https://www.acmicpc.net/problem/1309
1309번: 동물원
첫째 줄에 우리의 크기 N(1≤N≤100,000)이 주어진다.
www.acmicpc.net
저는 DP를 이용해 풀었습니다.
DP를 통해 이전에 계산한 것을 다시 계산하지 않도록하여 문제를 풀었습니다.
package BOJ.dp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BOJ_1309 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[][] dp = new int[n][3];
for(int i = 0 ; i < n ; i++){
Arrays.fill(dp[i],NOT_VALID);
}
int result = 0; // 아무것도 배치하지 않는 것
result += cal(0, 0, n,dp);
result += cal(0, 1, n,dp);
result += cal(0, 2, n,dp);
result %= LIMIT;
System.out.println(result);
}
private static final int LIMIT = 9901;
private static final int NOT_VALID = -1;
private static int cal(int y, int x, int n,int[][]dp) { // 0 1 2 - OX XO XX
if (y >= n-1) {
return 1;
}
if(dp[y][x] != NOT_VALID){
return dp[y][x];
}
int result = 0;
for(int i = 0 ; i < 3 ; i++){
if( (x == 0 && i == 0) || (x == 1 && i == 1)){
continue;
}
result += cal(y+1,i,n,dp);
result %= LIMIT;
}
dp[y][x] = result;
return result;
}
}
'알고리즘' 카테고리의 다른 글
백준 19621번 회의실 배정 2 (JAVA) (0) | 2023.12.31 |
---|---|
백준 1890번 점프 (JAVA) (0) | 2023.12.30 |
백준 2169번 로봇 조종하기 (JAVA) (0) | 2023.12.28 |
백준 10773번 제로 (JAVA) (1) | 2023.12.27 |
백준 15810번 풍선 공장 (JAVA) (0) | 2023.12.26 |