ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준][자바][1063]킹
    코딩/알고리즘 2018. 10. 2. 23:24

    와....이게 뭐라고...거의 푸는데 세시간 가까이 걸렸다..

    버그잡는데만 2시간을 쓴 것 같다..

    난  문제를 정말 잘 안 읽는다 

    좋은 설계가 있어야지 생고생을 안 하는듯 싶다...

    문제는 간단하다

    여기서 시뮬레이션 문제라 설명해주는 데로 구현하면 된다.

    나는 

    /*입력으로 주어진 대로 움직여서 킹이나 돌이 체스판 밖으로 나갈 경우에는 그 이동은 건너 뛰고 다음 이동을 한다.*/


    라는 부분을 주의 깊게 생각하지 않았는데 

    킹이 움직일 수 있어도 돌이 움직이지 못하면 말짱 꽝인 것이다.


    나는 킹부터 움직여놓고 돌이 움직이는게 가능한지를 했는데

    두개가 가능해야 둘다 움직인다. 라는 아주...아주 간단한 걸 분기처리를 안해놔서

    ...정말 고생을 했다..


    중간에 너무 지쳐서 리펙토링을 생각 못한...거의 레거시 코드라 창피하다..

    지금까지 코드들 전부 그렇겠지만. .ㅎ;

    import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; /*R : 한 칸 오른쪽으로 L : 한 칸 왼쪽으로 B : 한 칸 아래로 T : 한 칸 위로 RT : 오른쪽 위 대각선으로 LT : 왼쪽 위 대각선으로 RB : 오른쪽 아래 대각선으로 LB : 왼쪽 아래 대각선으로 */ class chess_Point{ int x; int y; chess_Point(int y, int x ){ this.x = x; this.y = y; } } public class backjoon_1063_킹 { static int[][] map = new int[8][8]; static Queue

    Postion_Q = new LinkedList(); static String[] alphabet = {"A","B","C","D","E","F","G","H"}; static int[] integ = {8,7,6,5,4,3,2,1}; static chess_Point king; static chess_Point stone ; public static void main(String[] args) { Scanner sc = new Scanner(System.in); /*input*/ String line = sc.nextLine(); String[] pos_Loop= line.split(" "); String king_Pos = pos_Loop[0]; String stone_Pos = pos_Loop[1]; king = cal_located(king_Pos); stone = cal_located(stone_Pos); int loop_Count = Integer.parseInt(pos_Loop[2]); for(int i = 0 ; i < loop_Count ; i++) { String pos = sc.next(); Postion_Q.add(pos); } /* MOVE */ /*입력으로 주어진 대로 움직여서 킹이나 돌이 체스판 밖으로 나갈 경우에는 그 이동은 건너 뛰고 다음 이동을 한다.*/ while( !Postion_Q.isEmpty() ) { String move = Postion_Q.poll(); chess_Point position = cal_Next_To_Go(move); // 다음 방향 이동 가능시 if ( isPossible(position.y, position.x, king) ) { if( ifStoneThere(king.y + position.y , king.x + position.x , position )) { king.y += position.y; king.x += position.x; } else { continue; } }else { //불가시 continue; } } System.out.println(alphabet[king.x]+ "" + integ[king.y]); System.out.println(alphabet[stone.x] + "" + integ[stone.y]); } /* 스톤 있는지 없는지 */ public static boolean ifStoneThere(int king_Sero , int king_Garo ,chess_Point cp) { // 있다면 같은 방향으로 이동 if( stone.x == king_Garo && stone.y == king_Sero ) { //스톤 범위 파악 if( isPossible(stone.y, stone.x , cp) ) { stone.y += cp.y; stone.x += cp.x; return true; } else { return false; } } return true; } /*다음 포인트 반환 */ public static chess_Point cal_Next_To_Go(String move) { if(move.equals("R")) return new chess_Point(0,1); if(move.equals("L")) return new chess_Point(0,-1); if(move.equals("B")) return new chess_Point(1,0); if(move.equals("T")) return new chess_Point(-1,0); if(move.equals("RT")) return new chess_Point(-1,1); if(move.equals("LT")) return new chess_Point(-1,-1); if(move.equals("RB")) return new chess_Point(1,1); if(move.equals("LB")) return new chess_Point(1,-1); return new chess_Point(0,0); } /* 베열 어레이 바운드 검사 */ //다음 방향 세로 ,다음 방향 가로 ,현재 킹 public static boolean isPossible(int sero , int garo , chess_Point cp) { int xx = cp.x + garo; int yy = cp.y + sero; if( yy >= 0 && yy < 8 && xx >= 0 && xx < 8 ) { return true; } else { return false; } } /*킹 위치 ,돌 위치 구하기 */ public static chess_Point cal_located(String str) { String[] arr = str.split(""); int garo = maping_Alp(arr[0]); int sero = Integer.parseInt((arr[1])); sero = maping_Int(sero); return new chess_Point(sero, garo); } /*알파벳이랑 맵핑 */ public static int maping_Alp(String alp) { int idx = 0 ; for (int i = 0 ; i < alphabet.length; i++){ if( alphabet[i].equals(alp)){ return i; } } return idx; } public static int maping_Int(int number) { for (int i = 0 ; i < integ.length; i++){ if( integ[i] == number ){ return i; } } return 0; } }


    '코딩 > 알고리즘' 카테고리의 다른 글

    [백준][자바][2146 다리] 해결못함  (0) 2018.10.07
    [백준][자바][5427 불]  (0) 2018.10.07
    [백준][자바][14502]연구소  (0) 2018.10.01
    [백준][3055][자바] 탈출  (0) 2018.09.30
    [백준][자바][5532]방학숙제  (0) 2018.09.15

    댓글

Designed by Tistory.