• ํŒจ์ŠคํŠธ์บ ํผ์Šค ์ฑŒ๋ฆฐ์ง€ 10์ผ์ฐจ

    2021. 11. 10.

    by. JuneBee

    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 input
    		
    		dfs(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 off
    			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]);
    			operator[i]++;
    			}
    		}
    		
    		
    	}
    
    	
    	
    
    }

    ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ

    DFS ์ƒ์„ฑ

    private static void dfs(int depth, int sum){...}
    //main ํ•จ์ˆ˜:
    dfs(0,0); //depth ์™€ sum

    depth ์™€ ํ˜„์žฌ 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 ์™€ sum
    		System.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 passwords
    		C = Integer.parseInt(st.nextToken()); //number of given characters
    		visited = new boolean[C];
    		//password : ์ตœ์†Œ ํ•œ๊ฐœ์˜ ๋ชจ์Œ๊ณผ ๋‘๊ฐœ์˜ ์ž์Œ์œผ๋กœ ๊ตฌ์„ฑ . alphabetically ordered ์ฆ๊ฐ€์ˆœ์œผ๋กœ ๊ตฌ์„ฑ
    		input = br.readLine().replace(" ", "").toCharArray();
    		Arrays.sort(input); //sort ํ•˜๋ฉด, ํ˜„์žฌ๊ฐ€ ์ด์ „๋ณด๋‹ค ์ž‘์€๊ฑฐ ๊ณ ๋ฏผํ•  ํ•„์š” x
    
    		char[] passwords = new char[L];
    		dfs(0,0 , passwords);//depth, index
    		bw.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
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€