Post

Baekjoon15649-N과M(1)

백준 사이트 15649 - N 과 M (1) 문제입니다.

1. 문제

https://www.acmicpc.net/problem/15649


2. Input , Output


3. 분류 및 난이도

백트래킹 문제입니다.
백준에서는 Sliver3의 난이도를 책정하고 있습니다.


4. 생각한 것들

  • 어떻게 해야할 지는 알겠으나, 백트래킹에 익숙하지 않은 저는 꽤 고전한 문제입니다.

5. code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

int n, m;
int arr[9];
bool v[9];

void Input()
{
	cin >> n >> m;
}

void Solve(int cnt) {

	if (cnt == m) {
		for (int i = 0; i < m; ++i)
			cout << arr[i] << " ";
		cout << '\n';
	}
	else
	{
		for (int i = 1; i <= n; ++i)
		{
			if (!v[i])
			{
				v[i] = true;
				arr[cnt] = i;
				Solve(cnt+1);
				v[i] = false;
			}
		}
	}
	
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Input();
	memset(v, false, sizeof(v));
	Solve(0);
}

6. 후기

백트래킹 연습해야겠다.

This post is licensed under CC BY 4.0 by the author.