μ•Œκ³ λ¦¬μ¦˜/λ°±μ€€

[ BOJ / λ°±μ€€ 17825 ] μ£Όμ‚¬μœ„ μœ·λ†€μ΄ ( μžλ°” / JAVA )

KIMHYEYUN 2022. 4. 15. 18:26
λ°˜μ‘ν˜•

https://www.acmicpc.net/problem/17825

 

17825번: μ£Όμ‚¬μœ„ μœ·λ†€μ΄

μ£Όμ‚¬μœ„ μœ·λ†€μ΄λŠ” λ‹€μŒκ³Ό 같은 κ²Œμž„νŒμ—μ„œ ν•˜λŠ” κ²Œμž„μ΄λ‹€. μ²˜μŒμ—λŠ” μ‹œμž‘ 칸에 말 4κ°œκ°€ μžˆλ‹€. 말은 κ²Œμž„νŒμ— κ·Έλ €μ§„ ν™”μ‚΄ν‘œμ˜ λ°©ν–₯λŒ€λ‘œλ§Œ 이동할 수 μžˆλ‹€. 말이 νŒŒλž€μƒ‰ μΉΈμ—μ„œ 이동을 μ‹œμž‘ν•˜λ©΄

www.acmicpc.net

풀이


지름길이 μ•„λ‹Œ 칸듀은 +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
λ°˜μ‘ν˜•