Algorithms/Java

[Implementation] 왕실의 나이트 (java)

Jenn28 2024. 5. 9. 01:05

[나의 코드]

public class Implementation2 {
    int result = 0;

    // 동 북 서 남
    // 2칸 이동
    int[] dx2 = {2, 0, -2, 0};
    int[] dy2 = {0, -2, 0, 2};

    // 1칸 이동
    int[] dx1 = {1, 0, -1, 0};
    int[] dy1 = {0, -1, 0, 1};

    public int solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        // 입력값 int로 변환
        int currX = input.charAt(0) - 'a' + 1;
        int currY = input.charAt(1) - '0';

        // 나이트 이동 - 첫번째 2칸 & 두번째 1칸
        for (int i = 0; i < 4; i++) {
            int nextX = currX + dx2[i];
            int nextY = currY + dy2[i];

            if (nextX >= 1 && nextX <= 8 && nextY >= 1 && nextY <= 8) {
                for (int j = 0; j < 4; j++) {
                    if (i == 0 && j == 2 || i == 0 && j == 0) {
                        continue;
                    }
                    else if (i == 1 && j == 3 || i == 1 && j == 1) {
                        continue;
                    }
                    else if (i == 2 && j == 0 || i == 2 && j == 2) {
                        continue;
                    }
                    else if (i == 3 && j == 1 || i == 3 && j == 3) {
                        continue;
                    }

                    int nextX2 = nextX + dx1[j];
                    int nextY2 = nextY + dy1[j];

                    if (nextX2 >= 1 && nextX2 <= 8 && nextY2 >= 1 && nextY2 <= 8) {
                        result += 1;
                    }
                }
            }
        }
        System.out.println(result);
        return result;
    }

    public static void main(String[] args) throws Exception {
        new Implementation2().solution();
    }
}

 

[나동빈님 코드]

public class Implementation2 {
    int result = 0;

    // 나이트가 이동할 수 있는 8가지 방향 정의
    int[] dx = {-2, -1, 1, 2, 2, 1, -1, -2};
    int[] dy = {-1, -2, -2, -1, 1, 2, 2, 1};

    public int solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        // 입력값 int로 변환
        int currX = input.charAt(0) - 'a' + 1;
        int currY = input.charAt(1) - '0';

        // 나이트 이동 - 첫번째 2칸 & 두번째 1칸
        for (int i = 0; i < 8; i++) {
            int nextX = currX + dx[i];
            int nextY = currY + dy[i];

            if (nextX >= 1 && nextX <= 8 && nextY >= 1 && nextY <=8) {
                result += 1;
            }
        }

        System.out.println(result);
        return result;
    }

    public static void main(String[] args) throws Exception {
        new Implementation2().solution();
    }
}

 

흑흑..... 어이없을 무....

방향을 8가지로 정의하면 조건문으로 검증하지 않아도 L자형 이동이 가능하다..

 

원래대로 4가지 방향으로 했다가 조건문을 사용할 수 밖에 없는 상황이 생김... ㅜㅜ

어쩐지 구현하면서 코드가 왤케 더러워지나 했다..

 

쨌든 좋았던 점은 구현 문제를 어려워해서 겁부터 먹고 시작했었는데

이 정도 풀어보니까 슬슬 감이 잡히는거 같다.

 

화이팅 !!