Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b34228

Browse files
committed
이예진: [BOJ] 28707 배열정렬_241126
1 parent b810971 commit 0b34228

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

‎BOJ/30000-35000번/YJ_28707.java‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
//2 <= N <= 8 으로 조합 경우의 수 8! > 40320개
5+
//1 <= A <= 10
6+
public class YJ_28707 {
7+
static class Controller {
8+
int l;
9+
int r;
10+
int c;
11+
12+
public Controller(int l, int r, int c) {
13+
this.l = l;
14+
this.r = r;
15+
this.c = c;
16+
}
17+
}
18+
19+
static class Node implements Comparable<Node>{
20+
int v;
21+
int c;
22+
23+
public Node(int v, int c) {
24+
this.v = v;
25+
this.c = c;
26+
}
27+
28+
@Override
29+
public int compareTo(Node o){
30+
return this.c - o.c;
31+
}
32+
}
33+
34+
static int n;
35+
static int answer;
36+
static List<Controller> list = new ArrayList<>();
37+
static Map<Integer,Integer> table = new HashMap<>(); //<배열,최단거리> 저장 테이블
38+
static PriorityQueue<Node> pq = new PriorityQueue<>();
39+
40+
public static void main(String[] args) throws IOException {
41+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
42+
n = Integer.parseInt(br.readLine());
43+
44+
//배열 입력값 초기화
45+
StringTokenizer st = new StringTokenizer(br.readLine());
46+
StringBuilder sb = new StringBuilder();
47+
int[] arr = new int[n];
48+
for(int i=0; i<n; i++){
49+
arr[i] = Integer.parseInt(st.nextToken());
50+
sb.append(arr[i]);
51+
}
52+
53+
//★정렬하는 배열 전체를 하나의 노드 단위로 생각
54+
int original = Integer.parseInt(sb.toString());
55+
//비내림차순 정렬 정답 저장
56+
Arrays.sort(arr);
57+
answer = arrToInt(arr);
58+
59+
//조작 입력값 초기화
60+
int m = Integer.parseInt(br.readLine());
61+
for(int i=0; i<m; i++){
62+
st = new StringTokenizer(br.readLine());
63+
int l = Integer.parseInt(st.nextToken())-1;
64+
int r = Integer.parseInt(st.nextToken())-1;
65+
int c = Integer.parseInt(st.nextToken());
66+
list.add(new Controller(l,r,c));
67+
}
68+
69+
table.put(original,0);
70+
pq.offer(new Node(original,0));
71+
dijkstra();
72+
System.out.println(table.getOrDefault(answer,-1));
73+
}
74+
75+
public static void dijkstra(){
76+
while(!pq.isEmpty()){
77+
Node node = pq.poll();
78+
if(answer == node.v){
79+
break;
80+
}
81+
//신규 비용이 기존 비용보다 더 클 경우 갱신불가
82+
if(table.containsKey(node.v) && table.get(node.v) < node.c){
83+
continue;
84+
}
85+
for(Controller controller : list){
86+
int next = swap(node.v, controller);
87+
int cost = node.c + controller.c;
88+
if(table.containsKey(next) && table.get(next) <= cost){
89+
continue;
90+
}
91+
//기존 비용+이동비용 이 기존의 최소 비용보다 더 작을 경우 최단거리(비용) 갱신
92+
table.put(next,cost);
93+
pq.offer(new Node(next,cost));
94+
}
95+
}
96+
}
97+
98+
private static int swap(int v, Controller controller){
99+
int[] arr = new int[n];
100+
for(int i = n - 1; i >= 0; i--) {
101+
if(v % 10 == 0){ // 10,20,30.. 등의 두 자릿수 배수가 있다는 뜻 (ex. 1410)
102+
arr[i] = v % 100; //두 자릿수를 배열에 저장
103+
v /= 100;
104+
} else{ //단일 자릿수일 경우 기존 자릿수 유지
105+
arr[i] = v % 10;
106+
v /= 10;
107+
}
108+
}
109+
110+
//조작에 맞춰서 자리 교체
111+
int l = controller.l;
112+
int r = controller.r;
113+
int temp = arr[l];
114+
arr[l] = arr[r];
115+
arr[r] = temp;
116+
117+
return arrToInt(arr);
118+
}
119+
120+
static StringBuilder sb = new StringBuilder();
121+
private static int arrToInt(int[] arr) {
122+
for(int num : arr){
123+
sb.append(num);
124+
}
125+
int result = Integer.parseInt(sb.toString());
126+
sb.setLength(0);
127+
return result;
128+
}
129+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /