제가 공부한 내용을 정리하는 블로그입니다.
아직 많이 부족하고 배울게 너무나도 많습니다. 틀린내용이 있으면 언제나 가감없이 말씀해주시면 감사하겠습니다😁
SWEA 1210 Ladder1입니다.

포인트

사다리 타기 게임에 포인트는 아래로만 향하다가 갈래를 만났을 때 무조건 그쪽 방향으로 간다는 것입니다.

100x100의 배열이기에 시작하는 위치(i==0이고 값이 1인) 위치를 기억하여 해당 위치에서 사다리를 탔을 때
목적지에 도착하도록 구현하는 문제입니다.

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Q1210 {
    static class Pair {
        int x, y, dir;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
            this.dir = 3;
        }

        public Pair(int x, int y, int dir) {
            this.x = x;
            this.y = y;
            this.dir = dir;
        }
    }
    private static int tc = 10;
    private static int[][] ladders;
    private static List<Pair> start = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        for (int t = 1; t <= tc; t++) {
            br.readLine();
            ladders = new int[100][100];
            visited = new boolean[100][100];
            start = new ArrayList<>();

            for (int i = 0; i < 100; i++) {
                st = new StringTokenizer(br.readLine());
                for (int j = 0; j < 100; j++) {
                    ladders[i][j] = Integer.parseInt(st.nextToken());
                    if (i == 0 && ladders[i][j] == 1) {
                        start.add(new Pair(i, j));
                    }
                }
            }

            sb.append("#").append(t).append(" ");
            for (int i = 0; i < start.size(); i++) {
                Pair cur = start.get(i);
                if (bfs(cur.x, cur.y)) {
                    sb.append(cur.y);
                }
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }

    private static boolean bfs(int startX, int startY) {
        int curX = startX;
        int curY = startY;

        while (curX < 100) {
            if (curY > 0 && ladders[curX][curY - 1] == 1) {
                // 왼쪽으로 쭉 감
                while (curY > 0 && ladders[curX][curY - 1] == 1) {
                    curY--;
                }
            } else if (curY < 99 && ladders[curX][curY + 1] == 1) {
                // 오른쪽으로 쭉 감
                while (curY < 99 && ladders[curX][curY + 1] == 1) {
                    curY++;
                }
            }
            curX++; // 아래로 한 칸
        }

        return ladders[curX - 1][curY] == 2;
    }

}

코드 설명

  • start 리스트에 시작 위치를 저장합니다.
  • simulate(int startX, int startY)
    사다리타기 게임하는 함수
    • curX가 100이라는 것은 마지막 위치에 도달했다는 의미이므로 종료조건으로 선언해줍니다.
    • 왼쪽에 1이 있을 경우(if (curY > 0 & ladders[curX][curY - 1] == 1)
      • 왼쪽으로 끝까지 이동합니다.
    • 오른쪽에 1이 있을 경우(if (curY < 99 & ladders[curX][curY + 1] == 1)
      • 오른쪽으로 끝까지 이동합니다.
    • 아래로 한칸 이동합니다.
    • 현재 칸이 2(목표지점)에 도달했는지 확인합니다.
      return ladders[curX - 1][curY] == 2

+ Recent posts