๋ฐ์ํ
https://www.acmicpc.net/problem/17825
ํ์ด
์ง๋ฆ๊ธธ์ด ์๋ ์นธ๋ค์ +2์ฉ ์ฆ๊ฐํ๊ณ ์๊ณ ,
์ง๋ฆ๊ธธ์ ๊ฐ์ง ์นธ, 10/20/30์ ๊ต์ฐจ์ ์ธ 25๋ก ํฅํ๋ค.
๊ทธ๋์ ๊ฐ ๋ ธ๋๋ ์ ์, ๋์ฐฉ์ง์ ์ธ์ง, ๋ง์ด ์๋์ง, ์ง๋ฆ๊ธธ ์ฌ๋ถ๋ฅผ ๊ฐ์ ธ์ผ ํ๋ค.
init()
์ง๋๋ฅผ ์์ฑํ๋ ํจ์ !!
์ ์ผ ๋ฐ๊นฅ์ชฝ ๊ฒฝ๋ก๋ฅผ ์์ฑํ๊ณ , ์ง๋ฆ๊ธธ์ ๊ฐ์ง 10/20/30 ์นธ์ ์ฐพ์ ์ง๋ฆ๊ธธ์ ์ถ๊ฐํด์ค๋ค!
permutation(int depth)
์ฃผ์ฌ์์ ๊ฐ ์ซ์๋ฅผ 1~4๋ฒ ๋ง์๊ฒ ํ ๋นํด์ฃผ๋ ํจ์ !
gameStart()
ํ ๋น๋ ์ซ์์ ์์๋ก ๋ง๋ค ์ด๋
์ฝ๋
import java.io.*;
import java.util.*;
public class Main_BOJ_17825_์ฃผ์ฌ์_์ท๋์ด {
static class Node{
int score;
boolean isEmpty;
boolean isFinish;
Node next;
Node fastPath;
public Node(int score) {
this.score = score;
this.isEmpty = true;
}
public Node addNext(int score) {
Node next = new Node(score);
this.next = next;
return next;
}
public static Node findNode(Node start, int target) {
Node tmp = start;
while (true) {
if(tmp == null) return null;
if(tmp.score == target) return tmp;
tmp = tmp.next;
}
}
}
private static int[] dice, order;
private static Node[] horses;
private static Node start;
private static int ans = Integer.MIN_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
dice = new int[11];
order = new int[11];
horses = new Node[5];
for (int i = 1; i <= 10; i++) {
dice[i] = Integer.parseInt(stringTokenizer.nextToken());
}
init();
permutation(1);
System.out.println(ans);
}
private static void permutation(int depth) {
if (depth == 11) {
ans = Math.max(ans, gameStart());
return;
}
for (int i = 1; i <= 4; i++) {
order[depth] = i;
permutation(depth + 1);
}
}
private static int gameStart() {
Arrays.fill(horses, start);
int score = 0;
for (int i = 1; i <= 10; i++) {
Node cur = horses[order[i]];
cur.isEmpty = true;
for (int j = 1; j <= dice[i]; j++) {
if (j == 1 && cur.fastPath != null) {
cur = cur.fastPath;
} else {
cur = cur.next;
}
}
horses[order[i]] = cur;
if (!cur.isEmpty && !cur.isFinish) {
score = 0;
break;
} else {
cur.isEmpty = false;
score += cur.score;
}
}
for (int i = 1; i <= 4; i++) {
horses[i].isEmpty = true;
}
return score;
}
private static void init() {
start = new Node(0);
Node tmp = start;
for (int i = 2; i <= 40; i += 2) {
tmp = tmp.addNext(i);
}
Node end = tmp.addNext(0);
end.isFinish = true;
end.next = end;
Node crossNode = new Node(25);
tmp = crossNode.addNext(30);
tmp = tmp.addNext(35);
tmp.next = Node.findNode(start, 40);
tmp = Node.findNode(start, 10);
tmp = tmp.fastPath = new Node(13);
tmp = tmp.addNext(16);
tmp = tmp.addNext(19);
tmp.next = crossNode;
tmp = Node.findNode(start, 20);
tmp = tmp.fastPath = new Node(22);
tmp = tmp.addNext(24);
tmp.next = crossNode;
tmp = Node.findNode(start, 30);
tmp = tmp.fastPath = new Node(28);
tmp = tmp.addNext(27);
tmp = tmp.addNext(26);
tmp.next = crossNode;
}
}
728x90
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ BOJ 2478 ] ์๋ฌผ์ (1) | 2023.11.06 |
---|---|
[ BOJ / ๋ฐฑ์ค ] 2048 EASY (JAVA / ์๋ฐ) (0) | 2022.09.28 |
[ ๋ฐฑ์ค / BOJ 14594 ] ๋๋ฐฉ ํ๋ก์ ํธ - Small ( ์๋ฐ / JAVA ) (0) | 2022.03.08 |
[ ๋ฐฑ์ค / BOJ 13460 ] ๊ตฌ์ฌ ํ์ถ 2 ( ์๋ฐ / JAVA ) (0) | 2022.03.06 |
[ ๋ฐฑ์ค / BOJ 22861 ] ํด๋ ์ ๋ฆฌ - large ( JAVA / ์๋ฐ ) (0) | 2022.02.22 |