백준_1063번_킹

임정민·2023년 2월 17일
2

알고리즘 문제풀이

목록 보기
35/173
post-thumbnail

코딩테스트 연습 스터디 진행중 입니다. ✍✍✍
Notion : https://www.notion.so/1c911ca6572e4513bd8ed091aa508d67

문제

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

[나의 풀이]

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

// 좌표 x,y 클래스
class Pos {

    int x;
    int y;
}

public class Main {
	
    // 문자좌표 <=> x,y좌표 변환 맵
    static Map<String, Integer> pos_parser = new HashMap<>();
 	// 문자 명령어 => 좌표 명령어 변환 맵
    static Map<String, int[]> cmd_parser = new HashMap<>();
	
    // 문자좌표 => x,y좌표 변환
    static Pos xy_parser(String info, Map pos_parser) {

        String alphabet = info.substring(0, 1);
        String num = info.substring(1, 2);

        Pos obj = new Pos();

        obj.x = (int) pos_parser.get(alphabet);
        obj.y = (int) pos_parser.get(num);

        return obj;
    }
	
    // x,y좌표 => 문자좌표 변환
    static String str_parser(Pos obj, Map pos_parser) {

        int alphabet = obj.x;
        int num = obj.y;

        String alphabet_key = "";
        String num_key = "";

        Boolean find_alphabet = false;

        for (Object key : pos_parser.keySet()) {

            if (!find_alphabet) {
                if (pos_parser.get((String) key).equals(alphabet)) {
                    alphabet_key = (String) key;
                    find_alphabet = true;
                }
            }
            if (find_alphabet) {
                if (pos_parser.get((String) key).equals(num)) {
                    num_key = (String) key;
                }
            }
        }

        return alphabet_key + num_key;
    }
	
    // 이동 명령 수행
    static Pos[] move_act(String cmd, Pos[] king_stone, Map cmd_parser) {

        int[] cmd_xy = (int[]) cmd_parser.get(cmd);
        Pos king = king_stone[0];
        Pos stone = king_stone[1];

        int next_king_x = king.x + cmd_xy[0];
        int next_king_y = king.y + cmd_xy[1];
        int next_stone_x = stone.x + cmd_xy[0];
        int next_stone_y = stone.y + cmd_xy[1];

        if (next_king_x >= 0 && next_king_y >= 0 && next_king_x <= 7 && next_king_y <= 7) {

            if (next_king_x == stone.x && next_king_y == stone.y) {
                if (next_stone_x >= 0 && next_stone_y >= 0 && next_stone_x <= 7 && next_stone_y <= 7) {
                    stone.x = next_stone_x;
                    stone.y = next_stone_y;
                } else {
                    return king_stone;
                }
            }

            king.x += cmd_xy[0];
            king.y += cmd_xy[1];

        }

        king_stone[0] = king;
        king_stone[1] = stone;

        return king_stone;
    }

    public static void main(String[] args) throws IOException {

        // 입력 받기
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        // 1) 킹,돌 위치
        st = new StringTokenizer(br.readLine());
        String[] info = new String[2];

        for (int i = 0; i < info.length; i++) {
            info[i] = st.nextToken();
        }

        // 2) 명령 갯수
        int num_cmd = Integer.parseInt(st.nextToken());
        String[] cmds = new String[num_cmd];

        // 3) 명령어 받기
        for (int i = 0; i < cmds.length; i++) {
            cmds[i] = br.readLine();
        }

        // board 만들기

        int[][] board = new int[8][8];

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                board[i][j] = 0;
            }
        }

        // pos_parser(변환 맵) 만들기

        Map<String, Integer> pos_parser = new HashMap<>();

        String num_alpha = "87654321ABCDEFGH";
        for (int i = 0; i < num_alpha.length(); i++) {
            pos_parser.put(num_alpha.substring(i, i + 1), (int) i % 8);
        }

        // 킹, 돌 처음위치로

        Pos king = new Pos();
        Pos stone = new Pos();

        king = xy_parser(info[0], pos_parser);
        stone = xy_parser(info[1], pos_parser);

        // 문자 명령 => 숫자 명령 맵

        Map<String, int[]> cmd_parser = new HashMap<>();
        int[] R = new int[] { 1, 0 };
        int[] L = new int[] { -1, 0 };
        int[] B = new int[] { 0, 1 };
        int[] T = new int[] { 0, -1 };
        int[] RT = new int[] { 1, -1 };
        int[] LT = new int[] { -1, -1 };
        int[] RB = new int[] { 1, 1 };
        int[] LB = new int[] { -1, 1 };

        cmd_parser.put("R", R);
        cmd_parser.put("L", L);
        cmd_parser.put("B", B);
        cmd_parser.put("T", T);
        cmd_parser.put("RT", RT);
        cmd_parser.put("LT", LT);
        cmd_parser.put("RB", RB);
        cmd_parser.put("LB", LB);
	
        // 명령 수행
        Pos[] king_stone = new Pos[] { king, stone };
        for (String cmd : cmds) {
            king_stone = move_act(cmd, king_stone, cmd_parser);
        }

        Pos ans_king = king_stone[0];
        Pos ans_stone = king_stone[1];
		
        // (x,y) 좌표 => 문자 좌표로 바꾸고 출력
        bw.write(str_parser(ans_king, pos_parser) + "\n");
        bw.write(str_parser(ans_stone, pos_parser));
        bw.flush();

    }

}

[팀원의 풀이]

if __name__ == '__main__' :
    import sys
    input = sys.stdin.readline

    k, r, n = list(input().split())
    # R L B T RT LT RB LB
    dx = [0,0,1,-1,-1,-1,1,1]
    dy = [1,-1,0,0,1,-1,1,-1]
    kx, ky = 8-int(k[1]), ord(k[0])-65
    rx, ry = 8-int(r[1]), ord(r[0])-65
    for _ in range(int(n)) :
        command = input().rstrip()
        if command == 'R' :
            command = 0
        if command == 'L' :
            command = 1
        if command == 'B' :
            command = 2
        if command == 'T' :
            command = 3
        if command == 'RT' :
            command = 4
        if command == 'LT' :
            command = 5
        if command == 'RB' :
            command = 6
        if command == 'LB' :
            command = 7
        nkx, nky = kx+dx[command], ky+dy[command]
        if 0<=nkx<8 and 0<=nky<8 :
            kx, ky = nkx, nky
        else :
            kx, ky = nkx-dx[command], nky-dy[command]
        if kx == rx and ky == ry :
            nrx, nry = rx+dx[command], ry+dy[command]
            if 0 <= nrx < 8 and 0 <= nry < 8:
                rx, ry = nrx, nry
            else:
                rx, ry = nrx - dx[command], nry - dy[command]
                kx, ky = nkx - dx[command], nky - dy[command]

    kx, rx = 8-kx, 8-rx
    ky, ry = chr(ky+65), chr(ry+65)
    print(ky, end="")
    print(kx)
    print(ry, end="")
    print(rx)

구현 문제입니다.🍄🍄🍄 이전에 풀었던

백준 2178번 미로탐색(https://velog.io/@min0731/%EB%B0%B1%EC%A4%802178%EB%AF%B8%EB%A1%9C%ED%83%90%EC%83%89)과

유사한 체스판 이동 문제입니다. 문제풀이 2가지 포인트는 좌표 변환과 돌위치입니다. 해당 문제에서는 0,0 0,1 0,2 ... 좌표를 A8, B8, C8...로 표기하기 때문에 자칫 헷갈릴 여지가 있습니다. 이는 처음 킹과 돌의 문자 좌표위치를 (x,y)로 변환해주고 이동할 때까지 (x,y)로 계산한 후 최종 출력 시에만 다시 (x,y)에서 문자좌표로 변환해주어 해결했습니다.🐸🐸🐸
또한 돌이 체스판에서 나가버리는 경우는 킹이 이동하기전에 돌의 위치를 먼저 파악하는 조건문을 넣어 해결했습니다.

이러한 구현 문제는 시간이 조금 오래걸리긴 하지만 재미는 있네요🐷🐷🐷

감사합니다.

profile
https://github.com/min731

1개의 댓글

comment-user-thumbnail
2023년 2월 28일

답글 달기