본문 바로가기

구름톤 챌린지

구름톤 챌린지[JAVA] 10일 차 학습 일기

구름톤 챌린지 GameJam

[느낀점]

결국 이 문제도 풀이를 보며 풀었다. 풀이를 보지 않고는 나는 도저히 풀 수 없는 레벨이였다. 처음에 이 문제를 풀었을 때는 코드가 현재 정답코드의 2배이상이였다. static 매서드로 함수를 만들어서 풀 생각을 못해서 각 goorm 과 player의 게임 을 2번 진행해서 2배가 되버린것이다. 풀이를 참고해가며 문제를 다 작성하고 제출을 했는데 컴파일 오류가 계속나는 것이다. 그래서 chatgpt에 물어보고 구글링을 1시간넘게 계속했다. chatgpt도 문제가 없다고 말해서 이유를 못찾다가 알고보니 매서드를 만들어서 쓰려면 static 으로 만들어야했는데 나는 public static void main 안에 다 담아버려서 그랬던거였다. 아래는 정답 코드이다.

 

[정답코드]

import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        int N = Integer.parseInt(st.nextToken());

        //goorm과 player의 시작 위치 생성
        st = new StringTokenizer(br.readLine(), " ");
        int[] goormPos = {Integer.parseInt(st.nextToken()) - 1, Integer.parseInt(st.nextToken()) - 1};
        //bolean 배열은 기본적으로 false로 초기화된다
        boolean[][] goormVisited = new boolean[N][N];
        st = new StringTokenizer(br.readLine(), " ");
        int[] playerPos = {Integer.parseInt(st.nextToken()) - 1, Integer.parseInt(st.nextToken()) - 1};
        boolean[][] playerVisited = new boolean[N][N];


        //보드판 생성
        String[][] board = new String[N][N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < N; j++) {
                board[i][j] = st.nextToken();
            }
        }
        int goormScore = move(goormPos, goormVisited, 1, board, N);
        int playerScore = move(playerPos, playerVisited, 1, board, N);

        if (goormScore > playerScore) {
            System.out.println("goorm " + goormScore);
        } else if (goormScore < playerScore) {
            System.out.println("player " + playerScore);
        }
        
    }

        //dx/dy 기법을 사용해 행렬에서 방향을 관리, 문제가 문자열형태로 주기때문에 map을 사용

    static HashMap<String, int[]> directions = new HashMap<String, int[]>() {
        {
            put("U", new int[]{-1, 0});
            put("D", new int[]{1, 0});
            put("L", new int[]{0, -1});
            put("R", new int[]{0, 1});
        }
    };


        //좌표에서 이동을 하다 보드판을 넘어갔을 경우 반대편으로 넘어가는 함수
        static int setPos(int a,int N) {
            if (a == -1) return N - 1;
            if(a == N) return 0;
            return a;
        }

        //게임을 진행하다 종료됬을때의 점수를 return 해주는 함수
        static int move(int[] pos, boolean[][] visited, int score, String[][] board, int N) {

            //말의 위치
            int x = pos[0];
            int y = pos[1];
            //방문한 위치는 true로 전환
            visited[x][y] = true;
            //score는 초기에 1점

            boolean flag = true;
            //flag가 false가 되면 while문 종료
            while(flag) {
                String command = board[x][y];
                //command의 첫번째 문자
                int distance = Integer.parseInt(command.substring(0, command.length() - 1));
                //command의 두번째 문자
                String direction = command.substring(command.length() - 1);
                //distance만큼 direction 방향으로 이동
                for(int i=0;i<distance;i++) {
                    //이 부분이 약간 이해가 헷갈린다
                    x+=directions.get(direction)[0];
                    y+=directions.get(direction)[1];
                    x = setPos(x, N);
                    y = setPos(y, N);

                    if(!visited[x][y]){
                        visited[x][y] = true;
                        score++;
                    } else {
                        flag = false;
                        break;
                    }
                }
            }
            return score;
        }

}