From 88bbede0af42fc2c4e1bc8b241a5d2cc244103f3 Mon Sep 17 00:00:00 2001 From: ssun <56533266+blaire-pi@users.noreply.github.com> Date: Thu, 24 Apr 2025 21:59:25 +0900 Subject: [PATCH] Create 1039 yerin.py --- BaekJoon/1039/yerin.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 BaekJoon/1039/yerin.py diff --git a/BaekJoon/1039/yerin.py b/BaekJoon/1039/yerin.py new file mode 100644 index 0000000..4de1ce9 --- /dev/null +++ b/BaekJoon/1039/yerin.py @@ -0,0 +1,35 @@ +from collections import deque + +n, k = map(int, input().split()) +n = str(n) +length = len(n) + +queue = deque() +queue.append((n, 0)) + +visited = [set() for _ in range(k + 1)] +visited[0].add(n) + +max_value = -1 + +while queue: + current, depth = queue.popleft() + + if depth == k: + max_value = max(max_value, int(current)) + continue + + for i in range(length): + for j in range(i + 1, length): + swapped = list(current) + swapped[i], swapped[j] = swapped[j], swapped[i] + + if swapped[0] == '0': + continue + + next_num = ''.join(swapped) + if next_num not in visited[depth + 1]: + visited[depth + 1].add(next_num) + queue.append((next_num, depth + 1)) + +print(max_value)