쵼쥬
쵼쥬의 개발공부 TIL
쵼쥬
전체 방문자
오늘
어제
  • 분류 전체보기 (276)
    • 코딩테스트 (192)
      • [알고리즘] 알고리즘 정리 (7)
      • [백준] 코딩테스트 연습 (126)
      • [프로그래머스] 코딩테스트 연습 (59)
    • Spring (71)
      • [인프런] 스프링 핵심 원리- 기본편 (9)
      • [인프런] 스프링 MVC 1 (6)
      • [인프런] 스프링 MVC 2 (4)
      • [인프런] 실전! 스프링 부트와 JPA 활용1 (7)
      • [인프런] 실전! 스프링 부트와 JPA 활용2 (5)
      • [인프런] 실전! 스프링 데이터 JPA (7)
      • [인프런] 실전! Querydsl (7)
      • JWT (5)
      • [인프런] Spring Cloud (17)
      • [인프런] Spring Batch (4)
    • Java (6)
      • [Java8] 모던인자바액션 (4)
      • [부스트코스] 웹 백엔드 (2)
      • [패스트캠퍼스] JAVA STREAM (0)
    • CS (6)
      • 디자인 패턴과 프로그래밍 패터다임 (2)
      • 네트워크 (4)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 타임리프
  • 알고리즘
  • 코딩테스트
  • jpa
  • Spring Data JPA
  • spring
  • 백분
  • 백준
  • 프로그래머스
  • MVC
  • 구현
  • 위클리 챌린지
  • 인프런
  • 부스트코스
  • querydsl
  • BFS
  • 누적합
  • 자바
  • 스프링
  • 비트마스킹

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

구슬 탈출2 - 13460번
코딩테스트/[백준] 코딩테스트 연습

구슬 탈출2 - 13460번

2022. 3. 10. 16:15


내 코드

package com.company;

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

public class Main {
    static int N, M, answer;
    static int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    static boolean[][][][] visited;
    static char[][] arr;

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

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        visited = new boolean[N][M][N][M];
        answer = -1;
        arr = new char[N][M];
        Location location = new Location(1, 0, 0, 0, 0);

        for (int i = 0; i < N; i++) {
            String s = br.readLine();
            for (int j = 0; j < M; j++) {
                arr[i][j] = s.charAt(j);
                if (arr[i][j] == 'R') {
                    arr[i][j] = '.';
                    location.RX = i;
                    location.RY = j;
                } else if (arr[i][j] == 'B') {
                    arr[i][j] = '.';
                    location.BX = i;
                    location.BY = j;
                }
            }
        }

        System.out.println(bfs(location));
    }

    static int bfs(Location location) {
        visited[location.BX][location.BY][location.RX][location.RY] = true;
        Queue<Location> q = new LinkedList<>();
        q.add(location);

        while (!q.isEmpty()) {
            Location lo = q.poll();

            if (lo.count > 10) {
                return -1;
            }

            for (int i = 0; i < 4; i++) {
                int nextBX = lo.BX;
                int nextBY = lo.BY;
                int nextRX = lo.RX;
                int nextRY = lo.RY;
                boolean BCheck = false;
                boolean RCheck = false;

                while (true) {
                    if (!BCheck && (nextBX + d[i][0] != nextRX || nextBY + d[i][1] != nextRY)
                            && arr[nextBX + d[i][0]][nextBY + d[i][1]] != '#') {
                        nextBX += d[i][0];
                        nextBY += d[i][1];
                        if (arr[nextBX][nextBY] == 'O') {
                            nextBX = -1;
                            nextBY = -1;
                            BCheck = true;
                        }
                        continue;
                    }
                    if (!RCheck && (nextRX + d[i][0] != nextBX || nextRY + d[i][1] != nextBY)
                            && arr[nextRX + d[i][0]][nextRY + d[i][1]] != '#') {
                        nextRX += d[i][0];
                        nextRY += d[i][1];
                        if (arr[nextRX][nextRY] == 'O') {
                            nextRX = -1;
                            nextRY = -1;
                            RCheck = true;
                        }
                        continue;
                    }
                    break;
                }

                if (BCheck) {
                    continue;
                }
                if (RCheck) {
                    return lo.count;
                }

                if (!visited[nextBX][nextBY][nextRX][nextRY]) {
                    visited[nextBX][nextBY][nextRX][nextRY] = true;
                    q.add(new Location(lo.count + 1, nextRX, nextRY, nextBX, nextBY));
                }
            }
        }
        return -1;
    }
}

class Location {
    int count, RX, RY, BX, BY;

    public Location(int count, int RX, int RY, int BX, int BY) {
        this.count = count;
        this.RX = RX;
        this.RY = RY;
        this.BX = BX;
        this.BY = BY;
    }
}

'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글

욕심쟁이 판다 - 1937번  (0) 2022.03.14
가스관 - 2931번  (0) 2022.03.13
통나무 옮기기  (0) 2022.03.10
2048 (Easy) - 12100번  (0) 2022.03.07
연구소 - 14502번  (0) 2022.03.04
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 욕심쟁이 판다 - 1937번
    • 가스관 - 2931번
    • 통나무 옮기기
    • 2048 (Easy) - 12100번
    쵼쥬
    쵼쥬

    티스토리툴바