01-09 04:37
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_61 ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_61 ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ

miinsun 2022. 3. 3. 01:01

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํžŒ ๊ตฌ์Šฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘ M๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

 

์ž…๋ ฅ - ์ฒซ ๋ฒˆ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(3<=N<=10)๊ณผ M(2<=M<=N)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

4 2

์ถœ๋ ฅ - ์ฒซ๋ฒˆ ์งธ ์ค„์— ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ ์ˆœ์„œ๋Š” ์‚ฌ์ „์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

1 2 
1 3 
1 4 
2 3 
2 4 
3 4

 

 

โœจ ์•„์ด๋””์–ด

โ€‹

DFS(Level, start)๋กœ ์žฌ๊ท€๋ฅผ ์ง„ํ–‰ํ•˜์ž. start์˜ ํฌ๊ธฐ๋ฅผ ๋งค๋ฒˆ ๋งค๋ฒˆ ๊ฐ์†Œ ์‹œํ‚ค๋ฉด์„œ ๋‚˜๋ฅผ ๋ฐฐ์ œ์‹œํ‚จ๋‹ค.

 

 

๐Ÿ’ป Solution.java

import java.util.*;

public class Main {
	static int n, m;
	static int[] combi;
	
	public void DFS(int L, int s) {
		if(L == m) {
			for(int x : combi) System.out.print(x + " ");
			System.out.println();
		}
		else {
			for(int i = s; i <= n; i++) {
				combi[L] = i;
				DFS(L+1, i+1);
			}
		}
 	}
	
	public static void main(String[] args){
		Main main = new Main();
		Scanner sc = new Scanner(System.in);

		n = sc.nextInt();
		m = sc.nextInt();
		
		combi = new int [m];
		
		main.DFS(0, 1);
		sc.close();
		return ;
	}
}
Comments