코딩 테스트 대비/Java 코딩테스트 - it 대기업 유제

[Simulation] 사다리 타기

nippycloud 2026. 2. 23. 20:01

 

내 풀이

import java.util.*;
import java.io.*;
public class Main {
    public static void main (String[] args) throws IOException {

        int[][] tc1 = {{1, 3}, {2, 4}, {1,4}};
        int[][] tc2 = {{1, 3, 5}, {1, 3, 6}, {2, 4}};
        int[][] tc3 = {{1, 5}, {2, 4, 7}, {1, 5, 7}, {2, 5, 7}};
        int[][] tc4 = {{1, 5, 8, 10}, {2, 4, 7}, {1, 5, 7, 9, 11}, {2, 5, 7, 10}, {3, 6, 8, 11}};

        char[] answer1 = function(tc1, 5);
        char[] answer2 = function(tc2, 7);
        char[] answer3 = function(tc3, 8);
        char[] answer4 = function(tc4, 12);

        if (Arrays.equals(answer1, new char[]{'D', 'B', 'A', 'C', 'E'})) {
            System.out.println("1 : O");
        }

        if (Arrays.equals(answer2, new char[]{'A', 'C', 'B', 'F', 'D', 'G', 'E'})) {
            System.out.println("2 : O");
        }

        if (Arrays.equals(answer3, new char[]{'C', 'A', 'B', 'F', 'D', 'E', 'H', 'G'})) {
            System.out.println("3 : O");
        }

        if (Arrays.equals(answer4, new char[]{'C', 'A', 'F', 'B', 'D', 'I', 'E', 'K', 'G', 'L', 'J', 'H'})) {
            System.out.println("4 : O");
        }

    }

    private static char[] function(int[][] tc, int n) {
        char[] answer = null;

        // 1. 사다리 만들기
        boolean[][] board = buildLadder(tc, n);

        // 2. 사다리 타기
        answer = startLadder(board);

        return answer;
    }

    private static boolean[][] buildLadder(int[][] tc, int n) {
        int depth = tc.length;

        boolean[][] board = new boolean[depth][n];

        for (int i = 0; i < depth; i++) {
            int[] arr = tc[i];

            for (int a : arr) {
                board[i][a - 1] = true;
            }
        }

        return board;
    }


    private static char[] startLadder(boolean[][] board) {
        int depth = board.length;
        int wide = board[0].length;
        char[] answer = new char[wide];

            for (int i = 0; i < wide; i++) {

                int[] dx = {i, 0}; // 시작점 , x좌표 y좌표

                while (dx[1]  < depth) {
                    // 1. 현재 위치에 가로줄이 있다면 오른쪽으로 이동
                    if (board[dx[1]][dx[0]]) {
                        dx[0]++; // x ++
                        dx[1]++;
                    }
                    // 2. 내 왼쪽 칸에 가로줄이 있다면 왼쪽으로 이동
                    else if (dx[0] > 0 && board[dx[1]][dx[0] - 1]) {
                        dx[0]--; // x --
                        dx[1]++;
                    }
                    // 3. 아무것도 없을 경우 아래로
                    else {
                        dx[1]++; // y++
                    }

                }
                answer[dx[0]] = (char) ('A' + i);
            }

        return answer;
    }
}

 

 

 

답안

import java.util.*;
import java.io.*;
public class Main {
    public static void main (String[] args) throws IOException {

        int[][] tc1 = {{1, 3}, {2, 4}, {1,4}};
        int[][] tc2 = {{1, 3, 5}, {1, 3, 6}, {2, 4}};
        int[][] tc3 = {{1, 5}, {2, 4, 7}, {1, 5, 7}, {2, 5, 7}};
        int[][] tc4 = {{1, 5, 8, 10}, {2, 4, 7}, {1, 5, 7, 9, 11}, {2, 5, 7, 10}, {3, 6, 8, 11}};

        char[] answer1 = function(tc1, 5);
        char[] answer2 = function(tc2, 7);
        char[] answer3 = function(tc3, 8);
        char[] answer4 = function(tc4, 12);

        if (Arrays.equals(answer1, new char[]{'D', 'B', 'A', 'C', 'E'})) {
            System.out.println("1 : O");
        }

        if (Arrays.equals(answer2, new char[]{'A', 'C', 'B', 'F', 'D', 'G', 'E'})) {
            System.out.println("2 : O");
        }

        if (Arrays.equals(answer3, new char[]{'C', 'A', 'B', 'F', 'D', 'E', 'H', 'G'})) {
            System.out.println("3 : O");
        }

        if (Arrays.equals(answer4, new char[]{'C', 'A', 'F', 'B', 'D', 'I', 'E', 'K', 'G', 'L', 'J', 'H'})) {
            System.out.println("4 : O");
        }
    }

    private static char[] function(int[][] tc, int n) {
        char[] answer = new char[n];
        char c = 'A';
        for (int i = 0; i < n; i++) {
            answer[i] = c++;
        }

        for (int i = 0; i < tc.length; i++) {
            int[] arr = tc[i];
            for (int a : arr) {
                swap(a - 1, answer);
            }
        }

        return answer;
    }
    private static void swap(int idx, char[] answer) {
        char temp = answer[idx];
        answer[idx] = answer[idx + 1];
        answer[idx + 1] = temp;
    }
}