๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 15649, 15650, 15651, 15652 N๊ณผ M

๋ฟŒ์•ผ._. 2022. 8. 3. 23:59

Silver III

๋ฌธ์ œ ์ถœ์ฒ˜:
https://www.acmicpc.net/problem/15649
https://www.acmicpc.net/problem/15650
https://www.acmicpc.net/problem/15651
https://www.acmicpc.net/problem/15652

 

 


< N๊ณผ M(1) >

๋ฌธ์ œ 

 

์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
- 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ ์ค‘๋ณต ์—†์ด M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 8)

 

์ถœ๋ ฅ 

 

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ ๋˜๋ฉฐ,
๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	static int [] arr;
	static int [] visited;
	
	public static void search(int n, int m, int idx) {
		if(idx==m) {
			for(int i=0; i<m; i++) {
				System.out.print(arr[i]+" ");
			}
			System.out.println();
			return;
		}
		for(int i=0; i<n; i++) {
			if(visited[i]==0) {
				visited[i]=1;
				arr[idx]=i+1;
				search(n,m,idx+1);
				visited[i]=0;
			}
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		
		int n=Integer.parseInt(st.nextToken());
		int m=Integer.parseInt(st.nextToken());
		
		arr=new int[m];
		visited=new int[n];
		
		search(n,m,0);

	}
}

 

  • main
    : n, m ์ž…๋ ฅ
    : ํ•จ์ˆ˜ ํ˜ธ์ถœ
  • search (์ด ๊ฐœ์ˆ˜, ๊ตฌํ•˜๋ ค๋Š” ๊ฐœ์ˆ˜, ํ˜„์žฌ ๊ตฌํ•œ ๊ฐœ์ˆ˜)
    : ๊ตฌํ•˜๋ ค๋Š” ๊ฐœ์ˆ˜์™€ ํ˜„์žฌ ๊ตฌํ•œ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์ˆ˜์—ด ์ถœ๋ ฅ
    : ์ด๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ์•„์ง ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฐ’์ด๋ผ๋ฉด ๊ทธ ๊ฐ’์„ ์‚ฌ์šฉํ•ด์ฃผ๊ณ  search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ค์Œ ๊ฐ’์„ ๊ตฌํ•ด์คŒ
    -> ๊ฐ’์„ ๊ตฌํ–ˆ๋‹ค๋ฉด ๋‹ค์Œ ์ˆ˜์—ด์„ ๊ตฌํ•  ๋•Œ ๊ทธ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก visited๋ฅผ ์›๋ž˜๋Œ€๋กœ ๋Œ๋ ค์คŒ


< N๊ณผ M(2) >

๋ฌธ์ œ 

 

์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
- 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ ์ค‘๋ณต ์—†์ด M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด
- ๊ณ ๋ฅธ ์ˆ˜์—ด์€ ์˜ค๋ฆ„์ฐจ์ˆœ์ด์–ด์•ผ ํ•œ๋‹ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 8)

 

์ถœ๋ ฅ 

 

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ ๋˜๋ฉฐ,
๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	static int[] arr;

	public static void search(int n, int m, int idx, int start) {
		if (idx == m) {
			for (int i = 0; i < m; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
			return;
		}
		for (int i = start; i < n; i++) {

			arr[idx] = i + 1;
			search(n, m, idx + 1, i + 1);

		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		arr = new int[m];

		int start = 0;
		search(n, m, 0, start);

	}
}

 

  • '๊ณ ๋ฅธ ์ˆ˜์—ด์€ ์˜ค๋ฆ„์ฐจ์ˆœ'์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ 1 2์™€ 2 1์€ ๊ฐ™๋‹ค.
    ์ด ๋ถ€๋ถ„์„ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•˜์—ฌ search ํ•จ์ˆ˜์—์„œ searchํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํ˜ธ์ถœํ•œ ์œ„์น˜์ธ i๋ฅผ ๋„˜๊น€์œผ๋กœ์จ ๊ทธ ์ „ ๊ฐ’์€ ๋ฝ‘์ง€ ๋ชปํ•˜๊ฒŒ ํ•ด ์ค€๋‹ค. 

< N๊ณผ M(3) >

๋ฌธ์ œ 

 

์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
- 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด
- ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณจ๋ผ๋„ ๋œ๋‹ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 7)

 

์ถœ๋ ฅ 

 

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ ๋˜๋ฉฐ,
๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {
	static int[] arr;
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	// bufferedWriter ์‚ฌ์šฉํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ ํ•ด๊ฒฐ

	public static void search(int n, int m, int idx) throws IOException {
		if (idx == m) {
			for (int i = 0; i < m; i++) {
				bw.write(arr[i] + " ");
			}
			bw.write("\n");
			return;
		}
		for (int i = 0; i < n; i++) {

			arr[idx] = i + 1;
			search(n, m, idx + 1);

		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		arr = new int[m];

		search(n, m, 0);

		bw.flush();

	}
}

 

  • ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณ ๋ฅด๊ธฐ ์œ„ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ์—์„œ visited๋ฅผ ์—†์•ด๋‹ค.

โ“ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ ์ด์œ ๋Š”?

์ถœ๋ ฅ๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด์—ˆ๋‹ค. BufferedWriter๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 


< N๊ณผ M(4) >

๋ฌธ์ œ 

 

์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
- 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด
- ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณจ๋ผ๋„ ๋œ๋‹ค.
- ๊ณ ๋ฅธ ์ˆ˜์—ด์€ ๋น„ ๋‚ด๋ฆผ์ฐจ์ˆœ์ด์–ด์•ผ ํ•œ๋‹ค.
  - ๊ธธ์ด๊ฐ€ K์ธ ์ˆ˜์—ด A๊ฐ€ A1 ≤ A2 ≤... ≤ AK-1 ≤ AK๋ฅผ ๋งŒ์กฑํ•˜๋ฉด, ๋น„ ๋‚ด๋ฆผ์ฐจ์ˆœ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 8)

 

์ถœ๋ ฅ 

 

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ ๋˜๋ฉฐ,
๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {
	static int[] arr;
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

	public static void search(int n, int m, int idx, int start) throws IOException {
		if (idx == m) {
			for (int i = 0; i < m; i++) {
				bw.write(arr[i] + " ");
			}
			bw.write("\n");
			return;
		}
		for (int i = start; i < n; i++) {

			arr[idx] = i + 1;
			search(n, m, idx + 1, i);

		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		arr = new int[m];
		
		int start=0;
		search(n, m, 0, start);

		bw.flush();

	}
}

 

  • ๋ฐ”๋กœ ์œ„ ๋ฌธ์ œ์—์„œ ๋น„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค. search ํ•จ์ˆ˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 


์ƒ๊ฐ๐Ÿค”

 

N๊ณผ M ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉฐ ๋ฐฑํŠธ๋ž˜ํ‚น์— ๋Œ€ํ•˜์—ฌ ์กฐ๊ธˆ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋ž˜๋„ ์–ด๋ ต๋‹ค!

 

- N๊ณผ M(1)

 

- N๊ณผ M(2)

 

- N๊ณผ M(3)

 

-N๊ณผ M(4)