본문 바로가기

알고리즘/백트래킹8

[백준 풀이_Java] 1759 암호 만들기 (골드5) 이제는 변형 문제도 풀 수 있게 되었다! 신나~! import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; public class Main { static int L; static int C; static int[] arrAnswer; static String[] arrAbc; static int cntAeiou; static int cntElse; static StringBuilder sb = new StringBuilder(); static String strAeiou = "aeiou"; public .. 2024. 2. 19.
[백준 풀이_Java] 73646155 N과 M (3) (실버 3) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int N; static int M; static int[] arrAnswer; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] input = br.readLine().s.. 2024. 2. 19.
[백준 풀이_Java] 15652 N과 M (4) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int N; static int M; static int[] arrAnswer; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] input = br.readLine().split(" "); N = Integer.parseInt(input[0]); M = .. 2024. 2. 16.
[백준 풀이_Java] 1182 부분수열의 합 (실버2) dfs 함수 안에서 반복문만 쓰는 문제만 풀다가 포함 or 미포함 여부를 재귀로 타고 들어가는 문제를 만나서 결국 다른 블로그와 영상을 보며 이해했다. 개수와 상관 없이 주어진 값을 조합해서 나온 결과를 확인할 때는, 해당 인덱스의 값을 포함할 것인지 아닌지를 코드로 구현하면 되겠다. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int N; static int S; static int result; static int[] arrNums; public static void main(String[] arg.. 2024. 2. 15.
[백준 풀이_Java] 73447483 로또 (실버2) 백트래킹을 사용해서 풀었다. 재귀함수 안에서 어떤 값을 올리느냐에 따라 결과가 달라진다는 것을 알았다. 여기서는 i=start 인데 i를 올릴지 start를 올릴지 잘 생각해야한다. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int[] arrNum; static int K; static int[] answer; static boolean[] visited; public static void main(String[] args) throws IOException { BufferedReader br = n.. 2024. 2. 15.
[백준 풀이_Java] 14888 연산자 끼워넣기 (실버1) 백트래킹으로 수를 연산하는 문제였는데 백트래킹 연습이 좀 더 필요할 것 같다. 핵심은 연산자를 주어진 개수대로 사용하고, 재귀함수를 타면서 주어진 수를 하나씩 사용해 연산하는 것이다. 물론, 재귀함수 호출 시 return 과, 결과물 하나를 만든 후 이전 상태로 복구하는 것 까지 고려해야한다. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int N; static int[] arithOpr; static int[] nums; public static int MAX = Integer.MIN_VALUE; p.. 2024. 2. 8.