-
728x90๋ฐ์ํ
์์ฒญ ๋ ์ง : 11/10/2021
์์ฒญ ๊ฐ์ : ์ด๋ป๊ฒ๋ ํผ๋ค. ์์ ํ์ (Brute Force) ์์ฉํธ์ค๋์ ์์ ํ์ ๊ฐ์ 2๋ฅผ ๋ฃ๊ธฐ ์ํด ๋ฌธ์ ๋ฅผ ํธ๋๊ฒ์ ์ค์ฌ์ผ๋ก ํ์ตํ๋ค.
์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ https://www.acmicpc.net/problem/14888 ๋ถ๋ถ์์ด์ ํฉ https://www.acmicpc.net/problem/1182 ์ํธ ๋ง๋ค๊ธฐ https://www.acmicpc.net/problem/1759 ์์ ์ฐ์ต๋ฌธ์ ๋ฅผ ๋ชจ๋ dfs๋ฅผ ์ฌ์ฉํ์ฌ ํ์๋ค.
์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ
๋ฌด๋ ค ์ผ์ฑ ๊ธฐ์ถ๋ฌธ์ ๋ก ๋ถ๋ฅ๋์ด์๋ค. ์ด๋ ๊ฒ๋ง ๋์ค๋ฉด ์ผ๋ง๋ ํ๋ณตํ ๊น..DFS
private static void dfs(int depth , int calculated){ ... }//main ํจ์:dfs(1,nums[0]);dfs ํจ์์ depth ์ ํ์ฌ ๊ณ์ฐ์ค์ธ int variable์ ํ๋ผ๋ฏธํฐ์ ๋๊ฒจ ์ฌ๊ท๋ก ํธ์ถํ์๋ค.
depth==N์์ ๋ฉ์ถ๊ธฐ ๋๋ฌธ์ ์ฒ์ depth๋ฅผ 0 ์ด์๋๋ผ 1๋ก ์ฃผ์๊ณ , ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด์ ๋งจ ์ฒ์ ์ซ์๋ฅผ ๋๊ฒจ์ฃผ์๋ค. (๋ง์ฝ, 4 ๋ผ๋ฉด 4 "+" "3" ์ด ๋์ด์ผํจ -> ๋งจ ์ฒ์ ์ซ์๋ ํ์!)
Return ์กฐ๊ฑด
if(depth == N) {min = Math.min(calculated, min );max = Math.max(calculated, max);return;}for loop
์ฒ์์๋ ์ฐ์ฐ์๊ฐ ์๋๋ผ, N ๋งํผ ๋๋ ค์ ์์ฒญ๋ ๋๋ฒ๊น ์ ํ๋ค.
for(int i = 0 ; i <4 ; i++) {//์ฐ์ฐ์์ ๋ํด +,-,*,/ ํ์ธif(operator[i]>0) { //์ฐ์ฐ์๊ฐ ๋จ์ ์์ ๊ฒฝ์ฐ์๋งoperator[i]--;//์ฌ์ฉ๋ ์ฐ์ฐ์ pop off//dfs ํธ์ถoperator[i]++;}}ํ์ง๋ง, operator๊ฐ ์ฌ์ด์ฆ๊ฐ 4๋ก ์กํ์๊ณ ,
operator [0] = +์ ๊ฐ์ , [1] = -์ ๊ฐ์ , [2] = *์ ๊ฐ์, [3] = /์ ๊ฐ์
๋ก ์ง์ ํด๋จ๋ค.
๋ฐ๋ผ์, ๋ฃจํ๋ฅผ 4๋งํผ ๋๋ ค์ผ ํ๊ณ , ํด๋น ์ฐ์ฐ์ด ๋จ์์๋ ๊ฒฝ์ฐ์๋ง dfs๋ฅผ ํธ์ถํด์ฃผ์๋ค. ์ด๋, operator[i]--๋ฅผ ์ฒ๋ฆฌํด์ฃผ์ด์ผ dfs๋ก ํจ์๋ฅผ ํธ์ถํ์ ๋ ๋ฐฉ๊ธ ์ฌ์ฉํ ์ฐ์ฐ์๋ฅผ ์ค๋ณต ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ์์ด์ง๊ณ ์ฌ๊ท์์ ๋์์ค๋ฉด ๋ค์ operator[i] ++ ๋ฅผ ํด์ค์ ์ด๊ธฐํ๋ฅผ ์์ผ์ผํ๋ค.
dfs ์กฐ๊ฑด
if(i==0) dfs(depth+1 , nums[depth]+calculated);else if(i==1) dfs(depth+1 , calculated - nums[depth]);else if(i==2) dfs(depth+1 , calculated * nums[depth]);else if(i==3) dfs(depth+1 , calculated / nums[depth]);i = 0์ผ๋๋ + ์ฐ์ฐ, 1์ผ๋๋ - ์ฐ์ฐ , 2 ์ผ๋๋ * ์ฐ์ฐ, 3 ์ผ๋๋ / ์ฐ์ฐ์ฒ๋ฆฌ๋ฅผ ํ๋ค. (operator[i] ์ i ์*)
dfs๋ฅผ ์ฌ๊ทํธ์ถํ ๋, depth๋ 1 ๋ํ๊ณ , calculated์ ๋ฐฉ๊ธ ์ฐ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ด ๋ณด๋ด์ ๋ค์๋ฒ ๊ณ์ฐ๋์๋ ์ธ ์ ์๊ฒ ํ๋ค.
์ ์ฒด ์์ค ์ฝ๋
package Backtracking;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;/*** @author June Park* @date 11/10/2021* dfs ๋ฅผ ์ด์ฉํ์ฌ ์ฐ์ฐ์ +-* /์ ๋ํด ๋จ์ ์ฐ์ฐ์๊ฐ ์๋ ๊ฒฝ์ฐ ํด๋น์ฐ์ฐ์ฒ๋ฆฌ ํ dfs๋ก ๋ณด๋ธ๋ค* */public class BOJ_14888_์ฐ์ฐ์๋ผ์๋ฃ๊ธฐ {static int N , nums[],operator[];static int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;public static void main(String[] args) throws Exception{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));N = Integer.parseInt(br.readLine());StringTokenizer st = new StringTokenizer(br.readLine());nums = new int[N];operator = new int[4];//+ - * /for(int i = 0 ; i < N ; i++) {nums[i] = Integer.parseInt(st.nextToken());}st = new StringTokenizer(br.readLine());for(int i = 0 ; i < 4 ; i++) {operator[i]=Integer.parseInt(st.nextToken());}//end of inputdfs(1, nums[0]);System.out.println(max);System.out.println(min);}private static void dfs(int depth , int calculated) {if(depth == N) {//System.out.println(calculated);min = Math.min(calculated, min );max = Math.max(calculated, max);return;}for(int i = 0 ; i <4 ; i++) {//์ฐ์ฐ์์ ๋ํด +,-,*,/ ํ์ธif(operator[i]>0) { //์ฐ์ฐ์๊ฐ ๋จ์ ์์ ๊ฒฝ์ฐ์๋งoperator[i]--; //์ฌ์ฉ๋ ์ฐ์ฐ์ pop offif(i==0) dfs(depth+1 , nums[depth]+calculated);else if(i==1) dfs(depth+1 , calculated - nums[depth]);else if(i==2) dfs(depth+1 , calculated * nums[depth]);else if(i==3) dfs(depth+1 , calculated / nums[depth]);operator[i]++;}}}}๋ถ๋ถ ์์ด์ ํฉ
DFS ์์ฑ
private static void dfs(int depth, int sum){...}//main ํจ์:dfs(0,0); //depth ์ sumdepth ์ ํ์ฌ dfs์ฐ์ฐ์ ํฉ์ ์ ์ฅํ ๋ณ์๋ฅผ ํ๋ผ๋ฏธํฐ์ ์ฃผ์๋ค.
Return ์กฐ๊ฑด
if(depth == N) { //๋๊น์ง ์ฐพ์๋ค๋ฉด,if(tempSum==S) {count++;}tempSum = 0;//์ด๊ธฐํreturn;}๋ง์ฝ, depth ๊ฐ N ์ ๊ฐ๋ค๋ฉด -> ๋ชจ๋ ์์๋ฅผ ๋๋ฌ๋ณด์๋จ ์๋ฆฌ๋ค.
๋ฐ๋ผ์, ์ฌํ๊น์ง์ ํฉ์ด ์ ๋ ฅ์ผ๋ก ๋ฐ์ target Sum (S)์ ๊ฐ์์ง ํ์ธํ๊ณ ๊ฐ๋ค๋ฉด, count ๋ฅผ ๋ํด์ค๋ค.
DFS ์กฐ๊ฑด
//๋ถ๋ถ์์ด. ์ง๊ธ ์์น์ ์์๋ฅผ ์ ํํ๊ฑฐ๋ ํ์ง ์๋๋คdfs(depth+1 , nums[depth]+tempSum);//์ ํํจdfs(depth+1 , tempSum); //์ ํ์ํจ๋ถ๋ถ์งํฉ์ ์ ํํ๊ฑฐ๋ ์ ํํ์ง ์๋ ๊ฒฝ์ฐ๋ก ํธ๋ฆฌ๋ฅผ ์ด๋ฃฌ๋ค.
์ด๋ฏธ์ง ์ถ์ฒ : ์ถค์ถ๋ ๊ฐ๋ฐ์ - JavaScript๋ก ๋ฉฑ์งํฉ(PoswerSet) ๋ฆฌํดํ๋ ํจ์ ๊ตฌํ๊ธฐ ์ ๊ทธ๋ฆผ์ ๋ณด๋ฉด, ์ข ๋ ์ดํดํ๊ธฐ ์ฌ์ธ ๊ฒ ๊ฐ์์ ๊ฐ์ ธ์๋ค.
ํ์ฌ ๋ฌธ์ ์์, ์ ํํ ๊ฒฝ์ฐ์๋ ํด๋น ์ซ์๋ฅผ ๋ํ๊ณ ์ ํํ์ง ์์ผ๋ฉด ๊ธฐ์กด ์ซ์๋ฅผ ํ๋ผ๋ฏธํฐ์ ๋๊ธด๋ค.
Count ์ฒ๋ฆฌ
count = S==0? -1 : 0;
์ dfs ํจ์๋ฅผ ์คํํ๊ธฐ์ , ์ ์ฝ๋๋ฅผ ์ฒ๋ฆฌํด์ฃผ์ด์ผ ํ๋ค. ๋ด ๋๋ฒ๊น ์๊ฐ์ ์ก์๋จน์ ์ฝ๋์ด๋ค...
๋ง์ฝ, ์ฃผ์ด์ง S๊ฐ 0 ์ธ ๊ฒฝ์ฐ๋ ๊ณต์งํฉ์ธ ๊ฒฝ์ฐ์ ํฉ๋ 0 ์ด๊ธฐ ๋๋ฌธ์ count๊ฐ ํ๋ ๋ ๋๊ฒ ๋๋ค.
์ด๊ฒ ๋ฐ๋ก ํ์๋งค๋ฌผ..๋ฐ๋ผ์, ๋ง์ฝ S๊ฐ 0์ด๋ผ๋ฉด -1๋ถํฐ ์์, ์๋๋ผ๋ฉด 0๋ถํฐ ์์ํด ์ฃผ๋ฉด ์ ์์ ์ผ๋ก ๊ณต์งํฉ์ ์ ์ธํ๊ณ ์นด์ดํธํ๊ฒ ๋๋ค.
์ ์ฒด ์์ค ์ฝ๋
package Backtracking;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;/*** @author June Park* @date 11/10/2021* ๋ถ๋ถ ์์ด์ ํฉ :dfs ๋ฅผ ์ฌ์ฉํ์ฌ ๋จผ์ ๋ถ๋ถ ์งํฉ์ ๊ตฌํ ํ,* return statement ์์ ํฉ์ด ๊ฐ์์ง ํ์ธํ๋ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์๋ค.*/public class BOJ_1182_๋ถ๋ถ์์ด์ํฉ {static int N, S , nums[], count;public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st= new StringTokenizer(br.readLine());N = Integer.parseInt(st.nextToken());S = Integer.parseInt(st.nextToken());nums= new int[N];st = new StringTokenizer(br.readLine());for(int i = 0 ; i < N ; i++) {nums[i]=Integer.parseInt(st.nextToken());}count = S==0? -1 : 0; //ํฉ์ด sum ์ด๋ฉด count ++ํด์ฃผ๊ณ , ๋ง์ฝ S =-0 ์ด๋ฉด ๊ณต์งํฉ์ ํฉ๋ 0์ด๊ธฐ ๋๋ฌธ์ 1๋นผ์ค๋คdfs(0,0); //depth ์ sumSystem.out.println(count);}/*** ๋ถ๋ถ ์งํฉ : ์ ํํ๊ฑฐ๋, ์ ํํ์ง ์๊ฑฐ๋* ์ ํํ์ ํธ๋ฆฌ๋ฅผ ์ด๋ฃฌ๋ค.* ์ฆ, dfs ๋ฅผ ์ฌ์ฉํ ๋, ํ์ฌ ์์๋ฅผ ์ ํํ(๋ํ) ์ฌ๊ทํธ์ถ๊ณผ ์ ํํ์ง ์์(๋ํ์ง ์์) ์ฌ๊ท ํธ์ถ์ ํ๋ค.* */private static void dfs(int depth, int tempSum) {if(depth == N) { //๋๊น์ง ์ฐพ์๋ค๋ฉด,if(tempSum==S) {count++;}tempSum = 0;return;}//๋ถ๋ถ์์ด. ์ง๊ธ ์์น์ ์์๋ฅผ ์ ํํ๊ฑฐ๋ ํ์ง ์๋๋คdfs(depth+1 , nums[depth]+tempSum);//์ ํํจdfs(depth+1 , tempSum); //์ ํ์ํจ}}์ํธ๋ง๋ค๊ธฐ
๋๋๊ฒ๋ ๋ช๋ฌ ์ ์ ํ์ด๋ณธ์ ์ด ์๋ ๋ฌธ์ ๋ค. ๊ทธ๋๋ dfs ๊ฐ ๋ญ์ง๋ ๋ชจ๋ฅด๊ณ ํ์๋๋ฐ ๋ค์ ์ฝ๋๋ฅผ ๋ณด๋ ์ง๊ธ๋ณด๋ค ํจ์ฌ ์ํ๋๊ฒ๊ฐ๋ค...
์ด๋ฒ์๋ ์ ํ์ ์ธ dfs๋ก ํ์ด๋ณด์๋ค.
Arrays.sort
Arrays.sort(input); //sort ํ๋ฉด, ํ์ฌ๊ฐ ์ด์ ๋ณด๋ค ์์๊ฑฐ ๊ณ ๋ฏผํ ํ์ x
์ด๋ถ๋ถ ๋๋ฌธ์ ํ์ฐธ ํด๋งธ๋ค.. ๊ณผ๊ฑฐ์ ๋๋ ๋๋๊ฒ๋ ์ด ๋ฌธ์ ๋ฅผ ๋ณด์๋ง์ ์ด ์๊ฐ์ ํ๋๋ณด๋ค.
๋น๋ฐ๋ฒํธ๋ ์ค๋ฆ์ฐจ์๋ง ๊ฐ๋ฅํ๋ค๊ณ ๋ฌธ์ ์์ ์ฃผ์ด์ ๊ธฐ ๋๋ฌธ์ ex : ABCD ๊ฐ๋ฅ, XASD ๋ถ๊ฐ๋ฅ
Arrays.sort๋ฅผ ์ฌ์ฉํ์ฌ ๋ฏธ๋ฆฌ ์ํ๋ฒณ ์์ผ๋ก ํ์ํ ๋ฌธ์ ๋ฐฐ์ด์ ์ ๋ ฌํด๋๋ฉด ๋ณ๋์ ์กฐ๊ฑด ์์ด๋ ํ์ํ ์ ์๋ค.
(ex : if(depth>0 && ์ด์ ๊ณ ๋ฅธ๋ฌธ์<์ ๋ ฅ๋ฐ์๋ฌธ์๋ฐฐ์ด[ํ์ฌ์ธ๋ฑ์ค]))
DFS ์์ฑ
private static void dfs(int depth, int index, char[] passwords){...}
Return ์กฐ๊ฑด
if(depth == L ) {int vowels=0 , consonants=0;for(char c : passwords) {if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') {vowels++;}else {consonants++;}}if(vowels>=1 & consonants>=2) {for(char c:passwords) {bw.write(c);}bw.write("\n");}return;}๊น์ด == length of password ๋ฉด,
1. ์กฐ๊ฑด์์ ์ ์ํ ๋ชจ์ 1๊ฐ์ด์๊ณผ ์์ 2๊ฐ์ด์ ์ ๋ง์กฑํ๋์ง ํ์ธํ๊ณ
2. ๋ง์กฑํ๋ฉด ์ ๋ต ๋ฌธ์์ด์ ์ถ๊ฐํ๋ค (์๋๊ฐ์ ์ ์ํด bw๋ฅผ ์ผ์ง๋ง, System.out.print๋ฅผ ์จ๋ ์๊ฐ ์ด๊ณผ๋ ๋์ง ์๋๋ค)
DFS ์กฐ๊ฑด
//์ค๋ณต ์ ํ ํ์ฉ x - visited ๋ฐฐ์ด ์ฌ์ฉfor(int i = index ; i < C ; i++) { //indexํ์ฉ ์ฑ ๊ฐํผif(visited[i])continue;visited[i] = true;passwords[depth] = input[i];dfs(depth+1 , i+1 , passwords);visited[i] = false;}ํจ์ค์๋ ์์ฑ ์, ์ค๋ณต๋ ๋ฌธ์๋ ํ์ฉ๋์ง ์๊ธฐ ๋๋ฌธ์ visited ๋ฐฐ์ด์ ์ฌ์ฉํ์๋ค.
๋ง์ฝ, ์ด๋ฏธ ์ฌ์ฉ๋ ๋ฌธ์๋ผ๋ฉด continue, ๊ทธ๋ ์ง ์๋ค๋ฉด ์ผ๋จ ์์ฑ์ค์ธ ํจ์ค์๋์ ๋ฌธ์๋ฅผ ๋ด๊ณ ์ฌ๊ท๋ฅผ ๋๋ ค์ validation ์ฒดํฌ๋ฅผ ํ๋๋ก ํ๋ค. ์ฌ๊ท๋ฅผ ํ ํ์ return statement๋ฅผ ๋ง๋๋ฉด ๋ค์ dfs(depth+1,i+1,passwords);์ง์ ์ ๋์์ค๊ฒ ๋๋๋ฐ, ๋ฐ๋ผ์ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ํด์ ์์ผ์ค์ผ ๋ค์๋ฒ ํจ์ค์๋๋ฅผ ์ ์์ ์ผ๋ก ๋ด์ ์ ์๋ค.
์ ์ฒด ์์ค ์ฝ๋
package Backtracking;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Arrays;import java.util.StringTokenizer;/***@ author June Park*@ date 11/10/2021*1. ์ค๋ฆ ์ฐจ์ : Arrays.sort()๋ฅผ ์ฌ์ฉํ๋ฉด, dfs์์ ์ถ๊ฐ ์ฐ์ฐ ์ํด๋ ๋๋ค. (greedy)*2. return ์กฐ๊ฑด์์ aeiou ๊ฐ์ ํ๋จ* */public class BOJ_1759_์ํธ๋ง๋ค๊ธฐ {static int L,C;static char[] input;static boolean[]visited;static BufferedWriter bw;public static void main(String[] args) throws Exception{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(br.readLine());bw = new BufferedWriter(new OutputStreamWriter(System.out));L = Integer.parseInt(st.nextToken()); //length of passwordsC = Integer.parseInt(st.nextToken()); //number of given charactersvisited = new boolean[C];//password : ์ต์ ํ๊ฐ์ ๋ชจ์๊ณผ ๋๊ฐ์ ์์์ผ๋ก ๊ตฌ์ฑ . alphabetically ordered ์ฆ๊ฐ์์ผ๋ก ๊ตฌ์ฑinput = br.readLine().replace(" ", "").toCharArray();Arrays.sort(input); //sort ํ๋ฉด, ํ์ฌ๊ฐ ์ด์ ๋ณด๋ค ์์๊ฑฐ ๊ณ ๋ฏผํ ํ์ xchar[] passwords = new char[L];dfs(0,0 , passwords);//depth, indexbw.flush();}//count ๋ก ๊ธธ์ด ์ผ๋คprivate static void dfs(int depth, int index, char[] passwords) throws IOException {if(depth == L ) {int vowels=0 , consonants=0;for(char c : passwords) {if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') {vowels++;}else {consonants++;}}if(vowels>=1 & consonants>=2) {for(char c:passwords) {bw.write(c);}bw.write("\n");}return;}//์ค๋ณต ์ ํ ํ์ฉ x - visited ๋ฐฐ์ด ์ฌ์ฉfor(int i = index ; i < C ; i++) { //indexํ์ฉ ์ฑ ๊ฐํผif(visited[i])continue;visited[i] = true;passwords[depth] = input[i];dfs(depth+1 , i+1 , passwords);visited[i] = false;}}}
ํจ์คํธ์บ ํผ์ค ํ๊ธ ์ฑ๋ฆฐ์ง ๋ฐ๋ก๊ฐ๊ธฐ๐ https://bit.ly/3FVdhDa
์๊ฐ๋ฃ 100% ํ๊ธ ์ฑ๋ฆฐ์ง | ํจ์คํธ์บ ํผ์ค
๋ฑ 5์ผ๊ฐ ์งํ๋๋ ํ๊ธ์ฑ๋ฆฐ์ง๋ก ์๊ฐ๋ฃ 100% ํ๊ธ๋ฐ์ผ์ธ์! ๋ ๋ฆ๊ธฐ์ ์ ์๊ธฐ๊ณ๋ฐ ๋ง์ฐจ ํ์น!
fastcampus.co.kr
๋ณธ ํฌ์คํ ์ ํจ์คํธ์บ ํผ์ค ํ๊ธ ์ฑ๋ฆฐ์ง ์ฐธ์ฌ๋ฅผ ์ํด ์์ฑ๋์์ต๋๋ค.
728x90๋ฐ์ํ'๐ STUDY > FASTCAMPUS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ์คํธ์บ ํผ์ค ์ฑ๋ฆฐ์ง 12์ผ์ฐจ (0) 2021.11.12 ํจ์คํธ์บ ํผ์ค ์ฑ๋ฆฐ์ง 11์ผ์ฐจ (0) 2021.11.11 ํจ์คํธ์บ ํผ์ค ์ฑ๋ฆฐ์ง 9์ผ์ฐจ (0) 2021.11.09 ํจ์คํธ์บ ํผ์ค ์ฑ๋ฆฐ์ง 8์ผ์ฐจ (0) 2021.11.08 ํจ์คํธ์บ ํผ์ค ์ฑ๋ฆฐ์ง 7์ผ์ฐจ (0) 2021.11.07