Coding 공부/Java

[Java_CodingGames] Shadows of the Knight - Episode 1

CBJH 2024. 3. 26. 18:03
728x90
반응형

목적 : 제한 라운드 안에 배트맨이 폭탄이 있는 좌표로 이동하면 승리한다.

 

규칙 :

점프를 할 때 마다, 열 감지 장치는 현재 위치를 기준으로 폭탄의 방향을 알려줄 것입니다. 당신의 임무는 장치를 프로그래밍하여 가능한 한 빨리 폭탄이 있는 방으로 이동할 다음 좌표로 이동하는 것입니다.

건물의 좌표는 직사각형 배열로 표현되며, 건물의 왼쪽 상단 모서리에 있는 창문은 인덱스 (0,0)에 있습니다.

 

position:

U (Up)
UR (Up-Right)
R (Right)
DR (Down-Right)
D (Down)
DL (Down-Left)
L (Left)
UL (Up-Left)

 

 

 

import java.util.*;;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Player {
    int x,y,w,h,n; 
    int yMax, yMin, xMax, xMin;
    private void down(){
        yMin = y;
        int deltaY = Math.max(1, (yMax-yMin)/2);
        y += deltaY;
    }

    private void up(){
        yMax = y;
        int deltaY = Math.max(1, (yMax-yMin)/2); 
        y -= deltaY;
    }

    private void right(){
        xMin = x;
        int deltaX = Math.max(1, (xMax-xMin)/2);
        x += deltaX;
    }

    private void left(){
        xMax = x;
        int deltaX = Math.max(1, (xMax-xMin)/2);
        x -= deltaX;
    }

    public static void main(String args[]) {
        Player p = new Player();        
        Scanner in = new Scanner(System.in);
        int W = in.nextInt(); // width of the building.
        int H = in.nextInt(); // height of the building.
        int N = in.nextInt(); // maximum number of turns before game over.
        int X0 = in.nextInt();
        int Y0 = in.nextInt();        
        p.x = X0; p.y = Y0; p.w = W; p.h = H; p.n = N;  //초기값 설정
        p.yMax = p.h; p.yMin = -1; p.xMax = p.w; p.xMin = -1; // 초기값 설정        
        // game loop
        while (true) {
            String bombDir = in.next(); 
            switch(bombDir){
                case "U":
                    p.up();         
                break;
                case "UR":
                    p.up();
                    p.right();       
                break;
                case "R":
                    p.right();                
                break;
                case "DR":
                    p.down();
                    p.right();          
                break;
                case "D":
                    p.down();                              
                break;
                case "DL":
                    p.down();
                    p.left();
                break;
                case "L":
                    p.left();     
                break;
                case "UL":
                    p.up();
                    p.left();
                break;
            }  
            System.out.printf("%d %d\n",p.x,p.y);            
        }
    }
}
  • 처음엔 x,y 좌표를 각각 컬렉션에 담아서 최소값과 최대값을 Collections 클래스의 메서드로 받아와 코딩을 했었는데, 코드가 길어져 가독성도 떨어지고 다른 사람이 만든 코드를 보니 right일 땐 x좌표의 최소값이 현재 x값이 된다는 점을 착안해서 코드를 수정했다.
  • 이동하는 좌표 deltaX와 deltaY값은 제한 라운드 안에 최대한 빨리 이동하기 위해 (xMax-xMin)/2 값 씩 이동해서 절반씩 점프했다. 
  • Math.max(1, 변수값); 메서드는 변수값이 0일 경우 1을 반환해주어 움직이지 않고 가만히 있는 상태를 방지했다.

 

코딩게임 사이트 링크 : https://www.codingame.com/ide/puzzle/shadows-of-the-knight-episode-1