알고리즘

백준 1063번 킹 (JAVA)

박카스마시며코딩 2023. 2. 2. 14:09

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

 

1063번: 킹

8*8크기의 체스판에 왕이 하나 있다. 킹의 현재 위치가 주어진다. 체스판에서 말의 위치는 다음과 같이 주어진다. 알파벳 하나와 숫자 하나로 이루어져 있는데, 알파벳은 열을 상징하고, 숫자는

www.acmicpc.net

 

package BOJ.simulation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_1063 {
    private static final char[] col = {'A','B','C','D','E','F','G','H'};
    private static final String[] DIR = {"T","R","B","L","RT","RB","LB","LT"};
    private static final int[] DY = {1,0,-1,0,1,-1,-1,1};
    private static final int[] DX = {0,1,0,-1,1,1,-1,-1};
    private static final int SIZE = 8;
    private static Function<String,Integer> stoi = Integer::parseInt;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int[] king = getPosition(st.nextToken());
        int[] block = getPosition(st.nextToken());
        int n = stoi.apply(st.nextToken());
        for(int i = 0 ; i < n ; i++){
            String command = br.readLine();
            int index = 0;
            for(int j = 0 ; j < 8 ; j++){
                if(command.equals(DIR[j])){
                    index = j;
                    break;
                }
            }
            int ny = king[0] + DY[index];
            int nx = king[1] + DX[index];
            if(!checkBound(ny,nx)){
                continue;
            }
            if(block[0] == ny && block[1] == nx){
                int bny = block[0] + DY[index];
                int bnx = block[1] + DX[index];
                if(!checkBound(bny,bnx)){
                    continue;
                }
                block = new int[] {bny,bnx};
                king = new int[] {ny,nx};
                continue;
            }
            king = new int[]{ny,nx};
        }
        StringBuilder sb = new StringBuilder();
        sb.append( (char)(king[1]+'A'-1) );
        sb.append(king[0]);
        sb.append('\n');
        sb.append( (char)(block[1]+'A'-1) );
        sb.append(block[0]);
        System.out.println(sb.toString());
    }

    private static int[] getPosition(String position) {
        int y = stoi.apply(position.charAt(1)+"");
        int x = position.charAt(0) - 'A' + 1;
        return new int[] {y,x};
    }
    private static boolean checkBound(int y, int x){
        if(y >= 1 && y <= SIZE && x >= 1 && x <= SIZE){
            return true;
        }else{
            return false;
        }
    }
}

 

 

'알고리즘' 카테고리의 다른 글

백준 15992번 1,2,3 더하기 7 (JAVA)  (0) 2023.02.04
백준 1331번 나이트 투어 (JAVA)  (0) 2023.02.03
백준 25644번 최대 상승 (JAVA)  (0) 2023.02.01
백준 새끼치기 (JAVA)  (0) 2023.01.31
백준 16918번 붐버맨 (JAVA)  (0) 2023.01.30