728x90
문제 링크
https://www.acmicpc.net/problem/12906
문제 설명
오늘은 새로운 하노이 탑 게임을 해보려고 한다. 이 게임의 규칙은 다음과 같다.
막대는 총 세 가지 종류가 있다. 막대 A, 막대 B, 막대 C
게임이 시작될 때, 각각의 막대에는 0개 또는 그 이상의 원판이 놓여져 있다.
모든 원판의 크기는 같으며, 원판의 종류도 A, B, C로 세 가지가 있다. 원판은 원판 A, 원판 B, 원판 C와 같이 표현한다.
한 번 움직이는 것은 한 막대의 가장 위에 있는 원판을 다른 막대의 가장 위로 옮기는 것이다.
게임의 목표는 막대 A에는 원판 A만, 막대 B는 원판 B만, 막대 C는 원판 C만 놓여져 있어야 한다.
되도록 최소로 움직여야 한다.
막대 A, 막대 B, 막대 C에 놓여져 있는 원판의 상태가 주어졌을 때, 게임의 목표를 달성하는데 필요한 움직임의 최소 횟수를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 막대 A에 놓여져 있는 원판의 개수와 막대 A의 상태, 둘째 줄에 막대 B에 놓여져 있는 원판의 개수와 막대 B의 상태, 셋째 줄에 막대 C에 놓여져 있는 원판의 개수와 막대 C의 상태가 주어진다. 막대의 상태는 밑에 있는 원판부터 주어진다.
각 막대의 상태는 A, B, C로만 이루어진 문자열이며, 모든 막대에 놓여져 있는 원판 개수의 합은 1보다 크거나 같고, 10보다 작거나 같다.
출력
게임의 목표를 달성하는데 필요한 움직임의 최소 횟수를 출력한다.
구조화
스택 + bfs
소스 코드
import java.io.*;
import java.util.*;
public class Main {
static class Node {
Stack<Character>[] h = new Stack[3];
public Node() {
for (int i = 0; i < 3; i++) {
h[i] = new Stack<>();
}
}
public String getNode() {
String str = "";
for (char c : h[0]) str += c;
str += '/';
for (char c : h[1]) str += c;
str += '/';
for (char c : h[2]) str += c;
str += '/';
return str;
}
}
static Set<String> set = new HashSet<>();
static String ans = "";
static char[] form = {'A','B','C'};
public static void main(String[] args) throws IOException {
Node node = new Node();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int[] cnt = new int[3];
for (int i = 0; i < 3; i++) {
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
if (t == 0) continue;
String str = st.nextToken();
for (int j = 0; j < t; j++) {
char tmp = str.charAt(j);
switch (tmp) {
case 'A' :
cnt[0]++;
break;
case 'B' :
cnt[1]++;
break;
case 'C' :
cnt[2]++;
break;
}
node.h[i].add(tmp);
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < cnt[i]; j++) {
ans += form[i];
}
ans += '/';
}
/*System.out.println(ans);
for (int i = 0; i < 3; i++) {
int size = node.h[i].size();
for (int j = 0; j < size; j++) {
System.out.print(node.h[i].pop()+" ");
}
System.out.println();
}*/
System.out.println(bfs(node));
}
static int bfs(Node start) {
Queue<Node> q = new LinkedList<>();
q.offer(start);
set.add(start.getNode());
int cnt = 0;
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
Node cur = q.poll();
if (cur.getNode().equals(ans)) {
return cnt;
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (!cur.h[i].isEmpty()) {
Node next = copy(cur);
next.h[j].push(next.h[i].pop());
String str = next.getNode();
if (!set.contains(str)) {
set.add(str);
q.offer(next);
}
}
if (!cur.h[j].isEmpty()) {
Node next = copy(cur);
next.h[i].push(next.h[j].pop());
String str = next.getNode();
if (!set.contains(str)) {
set.add(str);
q.offer(next);
}
}
}
}
}
cnt++;
}
return -1;
}
static Node copy(Node n) {
Node res = new Node();
for (int i = 0; i < 3; i++) {
for (char c : n.h[i]) res.h[i].push(c);
}
return res;
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 2698 인접한 비트의 개수 ✌️ (Java) (0) | 2024.11.10 |
---|---|
[백준] 16928 뱀과 사다리 게임 🐍 (Java) (1) | 2024.11.10 |
[백준] 16987 계란으로 계란치기 🥚 (Java) (0) | 2024.11.10 |
[백준] 16500 문자열 판별 🤔 (Java) (0) | 2024.11.10 |
[백준] 19699 소-난다! 🐮 (Java) (2) | 2024.11.10 |