오늘의 계획

  • 코드트리 문제풀이 - 완전탐색

문제 풀이

최적의 십자 모양 폭발

 

내 풀이

  • 각 칸을 돌면서 터뜨리고, 중력을 적용하고, 오른쪽과 아래에 같은 숫자가 있는지 탐색만하면 되는 쉬운 문제다.
import java.util.*;

public class Main {
    static int n;
    static int[][] grid;
    static int[][] after;
    static int ans = 0;
    static int[] dx = {0, 1, -1, 0};
    static int[] dy = {1, 0, 0, -1};

    public static void main(String[] args) {
        init();
        
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                clearAfter();
                solve(i, j);
                gravity();
                check();
            }
        }

        System.out.println(ans);
    }

    private static void check() {
        int cnt = 0;

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(after[i][j] == 0) continue;
                for(int k = 0; k < 2; k++) {
                    int nx = i + dx[k];
                    int ny = j + dy[k];

                    if(inRange(nx, ny)) {
                        if(after[nx][ny] == after[i][j]) cnt++;
                    }
                }
            }
        }

        ans = Math.max(ans, cnt);
    }

    private static void gravity() {
        for(int i = 0; i < n; i++) {
            int[] zero = {-1, -1};

            for(int j = n - 1; j >= 0; j--) {
                if(after[j][i] == 0) {
                    if(zero[0] != -1) continue;
                    zero[0] = j;
                    zero[1] = i;

                } else if(zero[0] != -1) {
                    after[zero[0]][zero[1]] = after[j][i];
                    after[j][i] = 0;
                    zero[0]--;
                }
            }
        }
    }

    private static void solve(int row, int col) {
        int frequency = grid[row][col];
        after[row][col] = 0;

        while(frequency-- > 0) {
            for(int i = 0; i < 4; i++) {
                int nx = row + (dx[i] * frequency);
                int ny = col + (dy[i] * frequency);

                if(inRange(nx, ny)) {
                    after[nx][ny] = 0;
                }
            }
        }
    }

    private static boolean inRange(int row, int col) {
        if(0 <= row && row < n && 0 <= col && col < n) return true;
        return false;
    }

    private static void clearAfter() {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                after[i][j] = grid[i][j];
            }
        }
    }

    private static void init() {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        grid = new int[n][n];
        after = new int[n][n];

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
        }
    }
}

핀볼게임

내 풀이

  • 처음에 30, 31번째 줄의 조건을 x를 기준으로 해서 의도와 다른 진행이 이뤄졌다.
  • 귀찮더라도 조건을 정확하게 기입하는 것이 필요할 것 같다.
import java.util.*;

public class Main {
    static int n;
    static int ans = 0;
    static int[][] grid;
    static int[] startX;
    static int[] startY;
    // 오, 왼, 위, 아래
    static int[] dx = {0, 0, -1, 1};
    static int[] dy = {1, -1, 0, 0};

    public static void main(String[] args) {
        init();

        for(int i = 0; i < 4; i++) {
            for(int j = 0; j < n; j++) {
                solve(i, j);
            }
        }

        System.out.println(ans);
    }

    private static void solve(int dir, int idx) {
        int time = 1;
        int x = startX[dir];
        int y = startY[dir];

        if(dir == 0 || dir == 1) x *= idx;
        if(dir == 2 || dir == 3) y *= idx;

        while(inRange(x, y)) {
            if(grid[x][y] == 1) dir = meetOne(dir);
            if(grid[x][y] == 2) dir = meetTwo(dir);

            x += dx[dir];
            y += dy[dir];

            time++;
        }

        ans = Math.max(time, ans);
    }

    private static int meetOne(int dir) {
        switch(dir) {
            case 0:
                return 2;
            case 1:
                return 3;
            case 2:
                return 0;
            case 3:
                return 1;
        }
        return dir;
    }

    private static int meetTwo(int dir) {
        switch(dir) {
            case 0:
                return 3;
            case 1:
                return 2;
            case 2:
                return 1;
            case 3:
                return 0;
        }
        return dir;
    }

    private static boolean inRange(int row, int col) {
        if(0 <= row && row < n && 0 <= col && col < n) return true;
        return false;
    }

    private static void init() {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        grid = new int[n][n];

        startX = new int[] {1, 1, n - 1, 0};
        startY = new int[] {0, n - 1, 1, 1};

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
        }
    }
}

+ Recent posts