๊ธฐ๋ก๋ฐฉ

BOJ_12100 : 2048 (Easy) ๋ณธ๋ฌธ

CodingTest/Java

BOJ_12100 : 2048 (Easy)

Soom_1n 2024. 3. 31. 21:06

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

12100๋ฒˆ: 2048 (Easy)

์ฒซ์งธ ์ค„์— ๋ณด๋“œ์˜ ํฌ๊ธฐ N (1 ≤ N ≤ 20)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฒŒ์ž„ํŒ์˜ ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ด์™ธ์˜ ๊ฐ’์€ ๋ชจ๋‘ ๋ธ”๋ก์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ธ”๋ก์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๋Š” 2

www.acmicpc.net


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N x N ๊ฒŒ์ž„ ํŒ์—์„œ 2048์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • 5๋ฒˆ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ์ตœ๋Œ€๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • 2048๊ฒŒ์ž„ ๊ทœ์น™์— ๋”ฐ๋ผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
    • ์ด๋™ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋ชจ๋“  ์นธ์˜ ์ˆซ์ž๊ฐ€ ์ด๋™ํ•œ๋‹ค.
    • ๊ฐ™์€ ์ˆซ์ž๋Š” ํ•ฉ์ณ์ง€๊ณ , ๊ฐ™์€ ํšŒ์ฐจ์—์„œ ๋”์ด์ƒ ํ•ฉ์ณ์ง€์ง€ ์•Š๋Š”๋‹ค.
    • ๋จผ์ € ์›€์ง์ธ ์ˆซ์ž๊ฐ€ ๋จผ์ € ํ•ฉ์ณ์ง„๋‹ค.
  • ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ธฐ์œ„ํ•ด 5ํšŒ ๊ฒŒ์ž„ ํšŸ์ˆ˜์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.
    • ํ’€์ด๋Š” DFS๋กœ ํƒ์ƒ‰ํ•˜์˜€๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    private static final int MAX = 5; // ์ตœ๋Œ€ ์ด๋™ ์ˆ˜
    private static int N, answer;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        N = Integer.parseInt(br.readLine());
        int[][] map = new int[N][N];

        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        answer = 0;
        play_2048(0, map);

        bw.write(Integer.toString(answer));
        bw.flush();
    }

    private static void play_2048(int cnt, int[][] map) {
//        print(map);
        if (cnt == MAX) {
            get_maxVal(map);
            return;
        }

//        System.out.println("up : " + (cnt + 1));
        play_2048(cnt + 1, move_up(copyMap(map), new boolean[N][N]));
//        System.out.println("down : " + (cnt + 1));
        play_2048(cnt + 1, move_down(copyMap(map), new boolean[N][N]));
//        System.out.println("left : " + (cnt + 1));
        play_2048(cnt + 1, move_left(copyMap(map), new boolean[N][N]));
//        System.out.println("right : " + (cnt + 1));
        play_2048(cnt + 1, move_right(copyMap(map), new boolean[N][N]));
    }


    private static int[][] move_up(int[][] map, boolean[][] flag) {
        for (int col = 0; col < N; col++) {
            for (int row = 1; row < N; row++) {

                int next = row;
                while (next - 1 >= 0 && !flag[next - 1][col]) {
                    if (map[next - 1][col] == 0)
                        next--;
                    else if (map[next - 1][col] == map[row][col]) {
                        next--;
                        break;
                    } else
                        break;
                }

                if (next != row && !flag[next][col]) {
                    if (map[next][col] == 0) { // ๋‹จ์ˆœ ์ด๋™
                        map[next][col] = map[row][col];
                        map[row][col] = 0;
                    } else if (map[next][col] == map[row][col]) { // ํ•ฉ์น˜๊ธฐ
                        map[next][col] *= 2;
                        map[row][col] = 0;
                        flag[next][col] = true;
                    }
                }
            }
        }

        return map;
    }

    private static int[][] move_down(int[][] map, boolean[][] flag) {
        for (int col = 0; col < N; col++) {
            for (int row = N - 2; row >= 0; row--) {

                int next = row;
                while (next + 1 < N && !flag[next + 1][col]) {
                    if (map[next + 1][col] == 0)
                        next++;
                    else if (map[next + 1][col] == map[row][col]) {
                        next++;
                        break;
                    } else
                        break;
                }

                if (next != row && !flag[next][col]) {
                    if (map[next][col] == 0) { // ๋‹จ์ˆœ ์ด๋™
                        map[next][col] = map[row][col];
                        map[row][col] = 0;
                    } else if (map[next][col] == map[row][col]) { // ํ•ฉ์น˜๊ธฐ
                        map[next][col] *= 2;
                        map[row][col] = 0;
                        flag[next][col] = true;
                    }
                }
            }
        }

        return map;
    }

    private static int[][] move_left(int[][] map, boolean[][] flag) {
        for (int row = 0; row < N; row++) {
            for (int col = 1; col < N; col++) {

                int next = col;
                while (next - 1 >= 0 && !flag[row][next - 1]) {
                    if (map[row][next - 1] == 0)
                        next--;
                    else if (map[row][next - 1] == map[row][col]) {
                        next--;
                        break;
                    } else
                        break;
                }

                if (next != col && !flag[row][next]) {
                    if (map[row][next] == 0) { // ๋‹จ์ˆœ ์ด๋™
                        map[row][next] = map[row][col];
                        map[row][col] = 0;
                    } else if (map[row][next] == map[row][col]) { // ํ•ฉ์น˜๊ธฐ
                        map[row][next] *= 2;
                        map[row][col] = 0;
                        flag[row][next] = true;
                    }
                }
            }
        }

        return map;
    }

    private static int[][] move_right(int[][] map, boolean[][] flag) {
        for (int row = 0; row < N; row++) {
            for (int col = N - 2; col >= 0; col--) {

                int next = col;
                while (next + 1 < N && !flag[row][next + 1]) {
                    if (map[row][next + 1] == 0)
                        next++;
                    else if (map[row][next + 1] == map[row][col]) {
                        next++;
                        break;
                    } else
                        break;
                }

                if (next != col && !flag[row][next]) {
                    if (map[row][next] == 0) { // ๋‹จ์ˆœ ์ด๋™
                        map[row][next] = map[row][col];
                        map[row][col] = 0;
                    } else if (map[row][next] == map[row][col]) { // ํ•ฉ์น˜๊ธฐ
                        map[row][next] *= 2;
                        map[row][col] = 0;
                        flag[row][next] = true;
                    }
                }
            }
        }

        return map;
    }

    private static int[][] copyMap(int[][] map) {
        int[][] copy = new int[N][N];
        for (int i = 0; i < N; i++) {
            System.arraycopy(map[i], 0, copy[i], 0, N);
        }
        return copy;
    }

    private static void get_maxVal(int[][] map) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                answer = Math.max(answer, map[i][j]);
            }
        }
    }

    private static void print(int[][] map) {
        StringBuilder sb = new StringBuilder();
        sb.append('\n');

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                sb.append(map[i][j]).append('\t');
            }
            sb.append('\n');
        }
        sb.append('\n');

        System.out.println(sb);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๊ฒŒ์ž„ํŒ ์ •๋ณด๋ฅผ 2์ฐจ์› intํ˜• ๋ฐฐ์—ด map์— ๋ฐ›์•„, dfs๋ฅผ ์ ์šฉํ•  ์žฌ๊ท€ ๋ฉ”์„œ๋“œ play_2048์— ๋„ฃ๊ณ  ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
    • ๊นŠ์ด ์ •๋ณด๊ฐ€ 5๊นŒ์ง€ ์˜ค๋ฉด ๋ชจ๋“  ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ•ด ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
    • ์•„์ง ๊นŠ์ด๊ฐ€ 5๊นŒ์ง€ ์˜ค์ง€ ์•Š์•˜์œผ๋ฉด, ์ƒํ•˜์ขŒ์šฐ ์ˆœ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
  • ์ƒํ•˜์ขŒ์šฐ ์ด๋™์€ ๊ฐ„๋žตํžˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ˆ์ฐจ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.
    • ์ด๋™ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐฉํ–ฅ์—์„œ ๊ฐ€๊นŒ์šด ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ด๋™์‹œ์ผœ ๋ณธ๋‹ค.
    • ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ธ๋ฑ์Šค ๋๊นŒ์ง€ ์ด๋™ํ•œ๋‹ค.
    • ์ด๋™ X / ๋‹จ์ˆœ ์ด๋™ / ํ•ฉ์น˜๊ธฐ ์ค‘์—์„œ ๋™์ž‘ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  ์ฒ˜์Œ์€ 14%์—์„œ ํ‹€๋ ธ๋Š”๋ฐ, ์˜ค๋ฅธ์ชฝ ์ด๋™์˜ ์กฐ๊ฑด์ด ํ‹€๋ ธ๋‹ค.
    • ๋‘ ๋ฒˆ์งธ๋Š” 2%์—์„œ ํ‹€๋ ธ๋Š”๋ฐ ๋‹ค์‹œ๋ณด๋‹ˆ ์•„๋ž˜ ์ด๋™์˜ ์กฐ๊ฑด๋„ ๊ฐ™์€ ๋ถ€๋ถ„์—์„œ ํ‹€๋ ธ๋‹ค.
    • ์ƒํ•˜์ขŒ์šฐ ๋น„์Šทํ•œ 4๊ฐ€์ง€ ๋ฒ„์ „์˜ ์ฝ”๋“œ๋ฅผ ํ•จ๊ป˜ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ๋‘ ์ข…๋ฅ˜์˜ ๋ณ€๊ฒฝ์ด ๋ฏธํกํ–ˆ๋‹ค.
    • ํ•œ ๋ฒˆ ํ‹€๋ ธ์œผ๋ฉด ๋‹ค๋ฅธ ์ข…๋ฅ˜๋„ ํ™•์ธํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, ํ’€์ด ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๋‹ค๋ณด๋‹ˆ ์ง‘์ค‘๋ ฅ์ด ํ๋ ค์ง„ ๊ฒƒ ๊ฐ™๋‹ค.
728x90