Skip to content

Commit be64530

Browse files
committed
백준 15654번 N과 M (5)
문제 링크: https://www.acmicpc.net/problem/15654
1 parent 23c6002 commit be64530

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

백준 15654번 N과 M (5)/main.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
from typing import List
3+
4+
5+
def solve():
6+
N, M = map(int, sys.stdin.readline().strip().split(' '))
7+
dt: List[int] = list(map(int, sys.stdin.readline().strip().split(' ')))
8+
dt.sort()
9+
10+
visited: List[bool] = [False for _ in range(len(dt))]
11+
my_perm(visited, dt, [], M, perm_found)
12+
13+
14+
def perm_found(perms: List[int]):
15+
perms = list(map(lambda x: str(x) + ' ', perms))
16+
perm = ''.join(perms).strip()
17+
print(perm)
18+
19+
20+
def my_perm(visited: List[bool], dt, perms, length, perm_found):
21+
if len(perms) == length:
22+
perm_found(perms)
23+
return
24+
25+
for i in range(len(dt)):
26+
if visited[i]:
27+
continue
28+
29+
visited[i] = True
30+
perms.append(dt[i])
31+
my_perm(visited, dt, perms, length, perm_found)
32+
perms.pop()
33+
visited[i] = False
34+
35+
36+
if __name__ == '__main__':
37+
solve()

백준 15654번 N과 M (5)/test1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3 1
2+
4 5 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2
2+
4
3+
5

백준 15654번 N과 M (5)/test2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4 2
2+
9 8 7 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1 7
2+
1 8
3+
1 9
4+
7 1
5+
7 8
6+
7 9
7+
8 1
8+
8 7
9+
8 9
10+
9 1
11+
9 7
12+
9 8

백준 15654번 N과 M (5)/test3.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4 4
2+
1231 1232 1233 1234
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1231 1232 1233 1234
2+
1231 1232 1234 1233
3+
1231 1233 1232 1234
4+
1231 1233 1234 1232
5+
1231 1234 1232 1233
6+
1231 1234 1233 1232
7+
1232 1231 1233 1234
8+
1232 1231 1234 1233
9+
1232 1233 1231 1234
10+
1232 1233 1234 1231
11+
1232 1234 1231 1233
12+
1232 1234 1233 1231
13+
1233 1231 1232 1234
14+
1233 1231 1234 1232
15+
1233 1232 1231 1234
16+
1233 1232 1234 1231
17+
1233 1234 1231 1232
18+
1233 1234 1232 1231
19+
1234 1231 1232 1233
20+
1234 1231 1233 1232
21+
1234 1232 1231 1233
22+
1234 1232 1233 1231
23+
1234 1233 1231 1232
24+
1234 1233 1232 1231
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
def my_solve(testcase_input):
8+
sys.stdin = open(testcase_input, 'r')
9+
stdout = sys.stdout
10+
sys.stdout = open('stdout.txt', 'w')
11+
solve()
12+
sys.stdout.close()
13+
sys.stdout = stdout
14+
15+
16+
class Test(TestCase):
17+
def test_solve(self, testcase_number: str):
18+
my_solve('test' + testcase_number + '.txt')
19+
self.assertEqual(
20+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
21+
Path('stdout.txt').read_text().strip())
22+
23+
def test1_solve(self):
24+
self.test_solve('1')
25+
26+
def test2_solve(self):
27+
self.test_solve('2')
28+
29+
def test3_solve(self):
30+
self.test_solve('3')
31+
32+
def test4_solve(self):
33+
self.test_solve('4')

0 commit comments

Comments
 (0)