olrlobt

[์ž๋ฐ”/๋ฐฑ์ค€ ๊ณจ๋“œ4] #14500 ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ๋ณธ๋ฌธ

Algorithm/๋ฐฑ์ค€

[์ž๋ฐ”/๋ฐฑ์ค€ ๊ณจ๋“œ4] #14500 ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

olrlobt 2022. 12. 30. 22:09

๐Ÿ”’ ๊ณจ๋“œ4 - #14500 ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

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

โœ๏ธ ํ’€์ด๋ฒ•

ํ•ด๋‹น๋ฌธ์ œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์„ํ•˜์ž๋ฉด, 4๊ฐœ์˜ ๋ถ™์–ด์žˆ๋Š” ์ˆซ์ž๋“ค์˜ ํ•ฉ์ด ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ฐพ๋Š” ๋ฌธ์ œ์˜€๋‹ค. 
๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ๋Œ์•„์•ผ ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— DFS๋ฅผ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ,
์‹œ์ž‘ ์ขŒํ‘œ์—์„œ๋ถ€ํ„ฐ DFS๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•ด์„œ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ ํ’€์—ˆ๋‹ค.

ํ’€์ด๋Š” ๊ฐ„๋‹จํžˆ ์ฒ˜์Œ ์‹œ์ž‘ํ•œ ์ž๋ฆฌ์—์„œ ์˜ค๋ฅธ์ชฝ > ์•„๋ž˜์ชฝ > ์™ผ์ชฝ ์ˆœ์œผ๋กœ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•˜๋ฉด ํ’€๊ฒ ๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ,
๋ฐ”๋กœ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 3๋ฒˆ์˜ ์˜ˆ์ œ์ฒ˜๋Ÿผ ใ… ใ…“ ใ…— ใ…œ ์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ƒ๊ฐ์„ ๋ชปํ•œ ํ’€์ด๋ฒ•์ด์—ˆ๊ณ ,
3๋ฒˆ์˜ ๊ฒฝ์šฐ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ solve() ํ•˜๋‹จ๋ถ€๋ถ„์˜ if ๋ฌธ์œผ๋กœ ๊น”๋”ํ•˜์ง€๋Š” ๋ชปํ•˜๊ฒŒ ํ•ด๊ฒฐํ•œ ๋ฌธ์ œ์˜€๋‹ค.

๐Ÿ—๏ธ ํ’€์ด

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Solution14500 {

    static List<Integer> answer = new ArrayList<>();
    static List<Integer> ar = new ArrayList<>();
    private static int N;
    private static int M;


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();
        M = sc.nextInt();

        for (int i = 0; i < N; i++) {   //์ž…๋ ฅ๋ฐ›๋Š” ๊ตฌ๋ฌธ
            for (int j = 0; j < M; j++) {
                ar.add(sc.nextInt());
            }
        }

        for (int k = 0; k < ar.size(); k++) { // solve
            List<Integer> tetro = new ArrayList<>();
            solve(k + 1, tetro);
        }

        int maxAnswer = Collections.max(answer);

        System.out.println(maxAnswer);
        sc.close();
    }

    private static void solve(int start, List<Integer> tetro) { // ์‹œ์ž‘์ง€์ ์—์„œ ์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜๋งŒ ์ฐพ๋Š”๋‹ค.

        tetro.add(start);

        if (tetro.size() == 4) { // ๋ฐฐ์—ด ๋ณต์‚ฌํ•ด์„œ ๋„˜๊ฒจ์ค˜์•ผํ• ๋“ฏ.
            answer.add(sumTetro(tetro));
            return;
        }
        if (validate(start + 1, 0, tetro)) {// ์šฐ์ธกํ•œ์นธ ๊ฐ€๋Šฅ ?
            List<Integer> copyTetro = new ArrayList<>(tetro);
            solve(start + 1, copyTetro);
        }
        if (validate(start + M, 1, tetro)) {// ์•„๋ž˜ํ•œ์นธ ๊ฐ€๋Šฅ ?
            List<Integer> copyTetro = new ArrayList<>(tetro);
            solve(start + M, copyTetro);
        }
        if (validate(start - 1, 2, tetro)) {// ์ขŒ์ธกํ•œ์นธ ๊ฐ€๋Šฅ ?
            List<Integer> copyTetro = new ArrayList<>(tetro);
            solve(start - 1, copyTetro);
        }

        if (tetro.size() == 3 && validateStick(tetro)) { // 3์—ฐ์† 1์ž๋กœ ๋ฐฐ์น˜๋ ๋•Œ, ์˜ˆ์™ธ ๊ฒ€์ฆ

            if (tetro.get(1) - tetro.get(0) == 1) {
                if (validate(tetro.get(1) + M, 1, tetro)) {// ใ…œ
                    List<Integer> copyTetro = new ArrayList<>(tetro);
                    solve(tetro.get(1) + M, copyTetro);
                }
                if (validate(tetro.get(1) - M, 3, tetro)) {// ใ…—
                    List<Integer> copyTetro = new ArrayList<>(tetro);
                    solve(tetro.get(1) - M, copyTetro);
                }
            } else { // ์„ธ๋กœ
                if (validate(tetro.get(1) + 1, 0, tetro)) {// ใ…
                    List<Integer> copyTetro = new ArrayList<>(tetro);
                    solve(tetro.get(1) + 1, copyTetro);
                }
                if (validate(tetro.get(1) - 1, 2, tetro)) {// ใ…“
                    List<Integer> copyTetro = new ArrayList<>(tetro);
                    solve(tetro.get(1) - 1, copyTetro);
                }
            }
        }
    }

    private static boolean validateStick(List<Integer> tetro) { // tetro์˜ ๊ฐ’๋“ค 3๊ฐœ๊ฐ€ 1์ž ๋ฐฐ์น˜์ผ๋•Œ,
        if (tetro.get(1) - tetro.get(0) == tetro.get(2) - tetro.get(1)) {
            return true;
        }
        return false;
    }

    private static Integer sumTetro(List<Integer> tetro) {
        int sum = 0;
        for (int i : tetro) {
            sum += ar.get(i - 1);
        }
        return sum;
    }

    private static boolean validate(int index, int direction, List<Integer> tetro) { // direction 0 ์˜ค๋ฅธ์ชฝ /1 ์•„๋ž˜/ 2์™ผ์ชฝ

        if (direction == 0 && index % M == 1) { // ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ”๋Š”๋ฐ, ์šฐ์ธก ๋์ผ๋•Œ.
            return false;
        }
        if (direction == 1 && index > N * M) { // ์•„๋ž˜
            return false;
        }
        if (direction == 2 && index % M == 0) { // ์™ผ์ชฝ
            return false;
        }
        if (direction == 3 && index < 1) { // ์œ„์ชฝ
            return false;
        }
        if (tetro.contains(index)) {
            return false;
        }
        return true;
    }
}

Comments