[๋ฐฑ์ค/1003/์ค๋ฒ3] ํผ๋ณด๋์น ํจ์
https://www.acmicpc.net/problem/1003
1003๋ฒ: ํผ๋ณด๋์น ํจ์
๊ฐ ํ ์คํธ ์ผ์ด์ค๋ง๋ค 0์ด ์ถ๋ ฅ๋๋ ํ์์ 1์ด ์ถ๋ ฅ๋๋ ํ์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด์ ์ถ๋ ฅํ๋ค.
www.acmicpc.net
<<ํ์ด>>
์ฐ์ ์ฒซ ๋ฒ์งธ ํ์ด๋ค. DFS๋ฅผ ํ์ฉํ๊ณ ๋ ๊ฐ์ ๋ฉ์๋๋ฅผ ํ์ฉํด์ ๋ฌธ์ ๋ฅผ ๊ตฌํ๋ค.
์ ๋ต์ ๋ง์ท๋ค !!!
๊ทผ๋ฐ, ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค ใ ใ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class Main {
static ArrayList<int[]> list = new ArrayList<>();
static int m, z;
static public void DFS(int x) {
if(x == 0) m++;
if(x == 1) z++;
if(x > 1) {
DFS(x - 1);
DFS(x - 2);
}
}
private ArrayList<int[]> solution(int n, int[] arr) {
for (int i = 0; i < n; i++) {
DFS(arr[i]);
list.add(new int[] {m, z});
m = 0;
z = 0;
}
return list;
}
public static void main(String[] args) throws Exception{
Main T = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int[] x : T.solution(n, arr)) {
for (int answer : x) {
System.out.print(answer + " ");
}
System.out.println();
}
}
}
๋ฉ๋ชจ์ ์ด์ ์ฌ์ฉ
๋ณดํต ์ฌ๊ธฐ์ ์๊ฐ ์ด๊ณผ๊ฐ ๋๋ ์ด์ ๋ ์ด๋ฏธ ๊ตฌํ ๋ฐ์ดํฐ๋ฅผ ๋ ๋ค์ ๊ตฌํ๋ฉด์ ์๊ฐ์ ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ๊ฒ์ด๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ฌ๊ทํจ์ ์์ฒด๊ฐ ์๊ฐ๋ถ๋ถ์์ ์ต์ ์ธ ์๊ณ ๋ฆฌ์ฆ์ธ๋ฐ ์ด๋ฅผ ์ต์ํ ํ๊ธฐ ์ํด์ ๋ฉ๋ชจ์ ์ด์ ์ ํ์ฉํ๋ค.
๋ฉ๋ชจ์ ์ด์ ์ ํ์ฉํ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐฐ์ด์ ๋ง์ด ์ฌ์ฉํ๋๋ฐ true false๋ก๋ ์ฌ์ฉํ๋ค.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class Main {
static ArrayList<int[]> list = new ArrayList<>();
static int m, z;
static int[][] ch;
static public void DFS(int x) {
if (ch[x][0] != 0 && ch[x][1] != 0) {
m += ch[x][0];
z += ch[x][1];
} else {
if (x == 0) m++;
if (x == 1) z++;
if (x > 1) {
DFS(x - 1);
DFS(x - 2);
}
}
}
private ArrayList<int[]> solution(int n, int[] arr) {
for (int i = 0; i < n; i++) {
DFS(arr[i]);
list.add(new int[] {m, z});
ch[arr[i]] = new int[] {m, z};
m = 0;
z = 0;
}
return list;
}
public static void main(String[] args) throws Exception{
Main T = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
ch = new int[41][2];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int[] x : T.solution(n, arr)) {
for (int answer : x) {
System.out.print(answer + " ");
}
System.out.println();
}
}
}
์ด๋ฒ์ ๋ฉ๋ชจ์ ์ด์ ์ ํ์ฉํด์ ๋ง์ฝ ์ด๋ฏธ ๊ตฌํ ๊ฐ์ด๋ผ๋ฉด ๊ทธ ๊ฐ์ ๋ถ๋ฌ์์ ์งํํ๋ ์์ผ๋ก ํ๋๋ฐ ,, ๋๋ค์ ์๊ฐ์ด๊ณผ์ด๋ค ใ ใ ใ ใ ใ
์ผ๋จ์ (0,0) ์ธ ๊ฒฝ์ฐ๋ ์ค๋ณต ๊ณ์ฐ์์ ๋ฒ์ด๋๊ฒ ํด์ค์ผ ํ๋ค. ๊ทธ๋์ ch ์ด๊ธฐ๊ฐ์ -1๋ก ๋ฐ๊ฟ์ผ๋ก์จ ํ ์ ์์๋ค.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class Main {
static ArrayList<int[]> list = new ArrayList<>();
static int m, z;
static int[][] ch;
static public void DFS(int x) {
if (ch[x][0] != -1 && ch[x][1] != -1) {
m += ch[x][0];
z += ch[x][1];
} else {
if (x == 0) m++;
if (x == 1) z++;
if (x > 1) {
DFS(x - 1);
DFS(x - 2);
}
}
}
private ArrayList<int[]> solution(int n, int[] arr) {
for (int i = 0; i < n; i++) {
DFS(arr[i]);
list.add(new int[] {m, z});
ch[arr[i]] = new int[] {m, z};
m = 0;
z = 0;
}
return list;
}
public static void main(String[] args) throws Exception{
Main T = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
ch = new int[41][2];
for (int i = 0; i < 41; i++) {
ch[i][0] = -1;
ch[i][1] = -1;
}
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int[] x : T.solution(n, arr)) {
for (int answer : x) {
System.out.print(answer + " ");
}
System.out.println();
}
}
}
๊ทธ๋๋ ์๊ฐ์ด๊ณผ ใ ใ ๋ฏธ์น๊ฒ๋ค ใ
DP ํ์ฉ
๊ฒฐ๊ตญ,,, DP๋ฅผ ํ์ฉํด์ ํ์ด๋ผ๋ ๋ฌธ์ ์๋ค ใ ใ ใ ใ
๊ทผ๋ฐ, ์์ง DP์ ๋ํ ๊ฐ๋ ์ด ๋ง์ด ์์ด์ ๊ณต๋ถํ๊ณ ๋ค์์๋ ์ด๊ฑธ ์ ๋๋ก ํ์ด๋ณด๋๋ก ํ๊ฒ ๋ค.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class Main {
static int[][] dp = new int[41][2]; // ๋ฉ๋ชจ์ด์ ์ด์
์ ์ํ ๋ฐฐ์ด
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine()); // ํ
์คํธ ์ผ์ด์ค์ ๊ฐ์
// dp ๋ฐฐ์ด ์ด๊ธฐํ
dp[0][0] = 1; dp[0][1] = 0; // fibonacci(0)์ 0์ 1๋ฒ, 1์ 0๋ฒ ๋ฐํ
dp[1][0] = 0; dp[1][1] = 1; // fibonacci(1)์ 0์ 0๋ฒ, 1์ 1๋ฒ ๋ฐํ
// fibonacci(2)๋ถํฐ fibonacci(40)๊น์ง ๊ณ์ฐ
for (int i = 2; i <= 40; i++) {
dp[i][0] = dp[i-1][0] + dp[i-2][0]; // 0 ๋ฐํ ํ์
dp[i][1] = dp[i-1][1] + dp[i-2][1]; // 1 ๋ฐํ ํ์
}
StringBuilder sb = new StringBuilder();
while (T-- > 0) {
int N = Integer.parseInt(br.readLine());
sb.append(dp[N][0]).append(' ').append(dp[N][1]).append('\n');
}
System.out.println(sb.toString());
}
}
<<์ถ๊ฐ ๊ณต๋ถ>>
ArrayList
๋ด๊ฐ ์ด๊ฑธ ์ ์ฉํ๋ค๊ฐ ์ข ์ ๋ฅผ ๋จน์๋ค. ๊ทธ ์ ํํ ์๋ฆฌ? ๊ตฌ์กฐ? ๋ฅผ ๋ชฐ๋๋ค๊ณ ๋ณผ ์ ์๋ค.
ArrayList๋ฅผ ์ด๊ธฐํ ์ํค๋ฉด null ์ด๊ณ 0์ด๊ณ ์๋ ์ํ์ด๋ค. ๊ทผ๋ฐ ํน์ ํ ์ธ๋ฑ์ค์ ์ ๊ทผํ๋ ค๊ณ ํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
๋๋ ์ด๋ฅผ ๋ชจ๋ฅด๊ณ ๋ฌด์์ ๋ฃ์ผ๋ ค๊ณ ํ๋ค.
๋ค์ ๋ถํฐ ์ด๋ฅผ ์ ์ฉํ ๋์๋ ๋จผ์ ๋ฃ๊ณ ๋ ๋ค ํด์ผ ํ๋ค๋ ๊ฒ์ ์์ง๋ง์!!!
ArrayList ๋ฉ์๋
set : ์กด์ฌํ๊ณ ์๋ ํน์ ์ธ๋ฑ์ค์ ๊ฐ์ ๋์ฒดํ ๋ ์ด๋ค.
add : ์กด์ฌํ๊ณ ์๋ ํน์ ์์น์ ์ธ๋ฑ์ค์ ๊ฐ์ ๋ฃ๊ณ ์๋ ์์น์ ์๋ ์ธ๋ฑ์ค์ ๊ฐ์ ๋ค๋ก ๋ฏธ๋ฃฌ๋ค.
int[][] arr = new int[5][5];
์ด๋ฌํ ๋ฐฐ์ด์ด ์๋ค๋ฉด ์ด๊ธฐํ๋ ๊ฐ ์์น๋ง๋ค ์ด๊ธฐํ 0์ด ๋๋ค.
int[][] arr = new int[5][];
์ด๋ ๊ฒ ํ๋ฉด arr[1], arr[2] .... ์ ๊ฐ์ null๋ก ๋๋ค. ๋๋ ์ด๋ถ๋ถ์ด ํท๊ฐ๋ ธ์ด์ ์๋ชป ์ ์ฉ์ ํ์๋ค.
'์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/2941/์ค๋ฒ5] ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (7) | 2024.03.05 |
---|---|
[DP] 1. ๊ณ๋จ์ค๋ฅด๊ธฐ (6) | 2024.03.02 |
[DFS] 7. ์กฐํฉ์ ๊ฒฝ์ฐ์(๋ฉ๋ชจ์ด์ ์ด์ ) (6) | 2024.02.26 |
[๋ฐฑ์ค/1002/์ค๋ฒ3] ํฐ๋ (3) | 2024.02.25 |
[DFS] 6. ์์ด ๊ตฌํ๊ธฐ (83) | 2024.02.24 |
๋๊ธ