Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 비트마스킹
- 플로이드 와샬
- 이분 매칭
- Gradle
- Zuul
- dp
- 유레카
- 서비스 디스커버리
- ZuulFilter
- BFS
- Logback
- 도커
- 달팽이
- 백트래킹
- docker-compose
- 이분 탐색
- 게이트웨이
- 스택
- 구간 트리
- Spring Cloud Config
- spring cloud
- 구현
- 메모이제이션
- spring boot
- 주울
- 트리
- 다익스트라
- 스프링 시큐리티
- Java
- 완전 탐색
Archives
- Today
- Total
Hello, Freakin world!
[백준 1010번] 다리 놓기 본문
https://www.acmicpc.net/problem/1010
1010번: 다리 놓기
입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트케이스에 대해 강의 서쪽과 동쪽에 있는 사이트의 개수 정수 N, M (0 < N ≤ M < 30)이 주어진다.
www.acmicpc.net
N의 첫번째 요소를 배정하는 위치하고 나서 나머지 N-1와 M-1에 대해서 부분구조를 발견할 수 있다.
점화식은
dp[n][m] = dp[n-1][1] + dp[n-1][2] ... + dp[n-1][m-1] 이다.
재귀 ver
package backjoon.p1010;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int T,N,M;
static long[][] dp;
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new FileReader("testcase.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
dp = new long[30][30];
for (int i = 1; i < 30; i++) {
dp[1][i] = i;
}
for (int testcase = 0; testcase < T; testcase++) {
String[] input = br.readLine().split(" ");
N = Integer.parseInt(input[0]); M = Integer.parseInt(input[1]);
long ret = solve(N, M);
System.out.println(ret);
}
}
private static long solve(int n, int m) {
if(n == 1) return dp[n][m];
if(dp[n][m] != 0) return dp[n][m];
long ret = 0;
for (int i = 1; i < m; i++) {
ret += solve(n-1, i);
}
dp[n][m] = ret;
return dp[n][m];
}
}
for문 version
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int T,N,M;
static long[][] dp;
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new FileReader("testcase.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
dp = new long[30][30];
for (int i = 1; i < 30; i++) {
dp[1][i] = i;
}
solve(29, 29);
for (int testcase = 0; testcase < T; testcase++) {
String[] input = br.readLine().split(" ");
N = Integer.parseInt(input[0]); M = Integer.parseInt(input[1]);
System.out.println(dp[N][M]);
}
}
private static long solve(int n, int m) {
if(dp[n][m] != 0) return dp[n][m];
for (int i = 2; i <= n; i++) {
for (int j = i; j <= m; j++) {
for (int k = 1; k < j; k++) {
dp[i][j] += dp[i-1][k];
}
}
}
return dp[n][m];
}
}
'알고리즘 > PS' 카테고리의 다른 글
[백준 1003번] Contact (0) | 2020.08.25 |
---|---|
[백준 1012] 유기농 배추 (0) | 2020.08.24 |
[백준 1009번] 분산 처리 (0) | 2020.08.23 |
[백준 1007번] 벡터 매칭 (0) | 2020.08.23 |
[백준 1006] 습격자 초라기 (0) | 2020.08.22 |
Comments