๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1039
๐ฎ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
์ฃผ์ด์ง ์ซ์ M์ ๋ํด์ ์๋ฆฟ์ ๊ตํ์ด K๋ฒ ์ผ์ด๋ฌ์ ๋ ๋์ฌ ์ ์๋ ์ต๋๊ฐ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
- ์๋ฆฟ์ i๋ณด๋ค ์๋ฆฟ์ j๊ฐ ๋ ์ปค์ผํ๋ ์ ํ ์กฐ๊ฑด์ด ์๊ธฐ ๋๋ฌธ์ ๊ตํํ ์ ์๋ ์๋ฆฟ์์ ์์์์ ๋ชจ๋ ๊ตฌํ๋ค.
- ๋ชจ๋ ๊ตฌํ ์์์๊ณผ ํจ๊ป ์ด๊ธฐ์ ์ฃผ์ด์ง M ๊ฐ๊ณผ K = 0์ธ ์ํ๋ก ํ์ ๋ฃ์ด์ค๋ค.
- ๋งค ํด ๋ง๋ค ์กด์ฌํ ์ ์๋ ๊ฐ์ ์์์์ ์ด์ฉํด ๊ตฌํ๋ค.
- ํ์ ์ฌ์ด์ฆ๋งํผ ๋ฐ๋ณตํ๋ค.
- ์์์์ ์ด์ฉํด ์๋ฆฟ์ ๊ตํ์ด ์ด๋ค์ก์ ๋ ๊ฐ์ ์ฒดํฌํ๋ค.
- ์ด๋ฏธ ํ์ฌ ํด์ ๊ตํํ ์ซ์๊ฐ ๋์๊ฑฐ๋ 0์ผ๋ก ์์ํ๋ ๊ฒฝ์ฐ์ ๊ฐ์ ํ์ ๋ฃ์ง ์๋๋ค.
- ๋ง์ฝ ํด์ ๋ชจ๋ ์ฌ์ฉํ ๊ฒฝ์ฐ์๋ Set์๋ค๊ฐ ๋ฃ์ด์ฃผ๊ณ ํ์๋ ๋ฃ์ง ์๋๋ค.
- ์ด์ธ์ ๊ฒฝ์ฐ๋ผ๋ฉด ๋ณ๊ฒฝํ ๊ฐ์ผ๋ก ๋ง๋ค ์ ์๋ ์์์์ ๋ฐ๋ณตํด์ ํ์ ๋ฃ์ด์ค๋ค.
- ๋ชจ๋ ํด์ ๋๊ณ Set์ ๋ค์ด๊ฐ ๊ฐ ์ค ์ต๋๊ฐ์ ๊ตฌํ๋ฉด๋๋ค.
โ๏ธ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฐ์ํ ๋ถ๋ถ์ ํ์์ ์ค๋ณต๋๋ ๊ฐ์ ์ฒดํฌ๋ฅผ ์ํด์ค์ ๋ฌ์๋ค. ์ค๋ณต์ ์ผ๋ก ๋์ค๋ ์ซ์๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํด ๋ฐฐ์ด์ ์ ์ธํด ์ฒดํฌํด์ฃผ๋ ๋ถ๋ถ์ด ์ฃผ์ํ๋ค.
๐ฉ Java ์ฝ๋
package com.second.algorithm.baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
public class Main_1039_๊ตํ {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
String N = st.nextToken();
int K = Integer.parseInt(st.nextToken());
int rst = changeNumber(N, K);
System.out.println(rst);
br.close();
}
private static int changeNumber(String n, int k) {
if(n.length() == 1) return -1;
if(n.length() == 2 && n.contains("0")) return -1;
// ์์์ ๊ตฌํ๊ธฐ
List<Pair> pairs = getPairs(n.length());
// ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๋ก ํ๋ก ๋ณํ
Queue<Possible> queue = pairs.stream()
.map(pair -> new Possible(n, pair.getFirst(), pair.getSecond(), 1))
.collect(Collectors.toCollection(LinkedList::new));
// ์
์ ์ด์ฉํด ์ค๋ณต ์ ๊ฑฐ
Set<String> available = new HashSet<>();
while (!queue.isEmpty()) {
int size = queue.size();
boolean[] check = new boolean[1000001];
// ํ ์ฌ์ด์ฆ๋งํผ ๋๋ฆฌ๊ธฐ
for (int loop = 0; loop < size; loop++) {
Possible possibleNumber = queue.poll();
String number = possibleNumber.getNumber();
int firstIndex = possibleNumber.getFirst();
int secondIndex = possibleNumber.getSecond();
int turn = possibleNumber.getTurn();
String afterSwap = swapNumber(firstIndex, secondIndex, number);
// ์ด๋ฏธ ํด๋น ํด์ ์ฒดํฌํ๊ฑฐ๋, 0์ผ๋ก ์์ํ๋ ๊ฒฝ์ฐ ์ ์ธ
if (afterSwap.startsWith("0") || check[Integer.parseInt(afterSwap)]) continue;
check[Integer.parseInt(afterSwap)] = true;
// ํด์ ๋ค ์ฌ์ฉํ ๊ฒฝ์ฐ
if (turn == k) {
available.add(afterSwap);
continue;
}
// ๋ณ๊ฒฝ๋ ๊ฐ ํ์ ๋ฃ๊ธฐ
for (Pair pair : pairs) {
queue.add(new Possible(afterSwap, pair.getFirst(), pair.getSecond(), turn + 1));
}
}
}
// ์ต๋๊ฐ ๊ตฌํ๊ธฐ
return available.stream().mapToInt(Integer::parseInt).max().getAsInt();
}
// ์ซ์ ๊ตํ
private static String swapNumber(int firstIndex, int secondIndex, String n) {
return n.substring(0, firstIndex) + n.charAt(secondIndex) +
n.substring(firstIndex + 1, secondIndex) + n.charAt(firstIndex) + n.substring(secondIndex + 1);
}
// ์๋ฆฟ์ ๊ตํ ๊ฐ๋ฅ ๊ฒฝ์ฐ์ ์ ๋ด๊ธฐ
private static List<Pair> getPairs(int m) {
List<Pair> pairs = new ArrayList<>();
for (int first = 0; first < m; first++) {
for (int second = first + 1; second < m; second++) {
pairs.add(new Pair(first, second));
}
}
return pairs;
}
static class Pair {
private int first;
private int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
static class Possible {
private String number;
private int first;
private int second;
private int turn;
public Possible(String number, int first, int second, int turn) {
this.number = number;
this.first = first;
this.second = second;
this.turn = turn;
}
public String getNumber() {
return number;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
public int getTurn() {
return turn;
}
}
}
๋ฐ์ํ
'Algorithm > BFS & DFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค, Java] 14226๋ฒ : ์ด๋ชจํฐ์ฝ (0) | 2022.07.27 |
---|---|
[๋ฐฑ์ค, Java] 19621๋ฒ : ํ์์ค ๋ฐฐ์ 2 (0) | 2022.05.23 |
[๋ฐฑ์ค, Java] 9934๋ฒ : ์์ ์ด์ง ํธ๋ฆฌ (0) | 2022.05.20 |
[๋ฐฑ์ค, Java] 16928๋ฒ : ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์ (0) | 2022.05.09 |
[๋ฐฑ์ค, Java] 1967๋ฒ : ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2022.05.08 |