팡세영
Log sey
팡세영
전체 방문자
오늘
어제
  • 분류 전체보기 (73)
    • PS (45)
      • programmers (13)
      • 백준 (29)
    • Android (16)
    • Daily (0)
    • Kotlin (6)
    • Design Pattern (1)
    • Java (1)
    • Flutter (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 코틀린
  • 해쉬맵
  • java
  • programmers #프로그래머스
  • binding
  • mvvm
  • 자바
  • 문자열
  • 구현
  • flutter
  • 이분탐색
  • programmers
  • 프로그래머스
  • CustomView
  • 의존성 주입
  • LEVEL2
  • Kotlin
  • DFS
  • 하단네비게이션바
  • Android
  • 안드로이드
  • 골드
  • 정렬
  • 실버
  • compose
  • BFS
  • 백준
  • ArcitecturePattern
  • 완전탐색
  • TestCode

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
팡세영

Log sey

[프로그래머스] - 미로탈출
PS/programmers

[프로그래머스] - 미로탈출

2023. 2. 16. 18:34


  • 시작 지점에서 레버까지 bfs로 최단거리 구한 후 도달하지 못하면 -1 리턴 
  • 레버에서 출구까지 bfs로 최단거리 구한 후 도달하지 못하면 -1 리턴
  • 레버 출구 둘다 갈 수 있으면 첫 번째 두 번째 최단거리를 더한후 리턴 

[Java]

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    char [][] map;
    int [][] depth;

    int width = 0;
    int height = 0;

    int dx[] = {0, 0, -1, 1};
    int dy[] = {-1, 1, 0 ,0};

    public int solution(String[] maps) {
        int answer = 0;

        width = maps[0].length();
        height = maps.length;

        // init map
        map = new char[height][width];
        depth = new int[height][width];

        for(int i=0; i<height; i++) Arrays.fill(depth[i], -1);

        for(int i=0; i<height; i++)
            for(int j=0; j<width; j++)
                map[i][j] = maps[i].charAt(j);


        Pos start = findIndex('S');
        Pos lever = findIndex('L');
        Pos exit = findIndex('E');

        bfs(start, lever);
        answer = depth[lever.y][lever.x];

        if(answer == -1) return -1;

        depth = new int[height][width];
        for(int i=0; i<height; i++) Arrays.fill(depth[i], -1);
        bfs(lever, exit);

        if(depth[exit.y][exit.x] == -1) return -1;
        answer += depth[exit.y][exit.x];

        return answer;
    }

    public void bfs(Pos start, Pos endPoint){
        Queue<Pos> q = new LinkedList<>();
        q.add(start);
        depth[start.y][start.x] = 0;

        if(start.x == endPoint.x && start.y == endPoint.y)
            return;

        while(!q.isEmpty()){
            Pos cur = q.poll();

            for(int i=0; i<4; i++){
                int nx = cur.x + dx[i];
                int ny = cur.y + dy[i];

                if(check(nx, ny) && depth[ny][nx] == -1){
                    q.add(new Pos(ny, nx));
                    depth[ny][nx] = depth[cur.y][cur.x] + 1;
                }

                if(endPoint.x == nx && endPoint.y == ny) return;
            }

        }
    }

    public boolean check(int nx, int ny){
        return (nx >= 0 && nx < width && ny >=0 && ny < height && map[ny][nx] != 'X') ? true : false;
    }

    public Pos findIndex(char c){
        for(int i=0; i<map.length; i++){
            for(int j=0; j<map[0].length; j++){
                if(c == map[i][j]) return new Pos(i, j);
            }
        }
        return new Pos(-1,-1);
    }
}

class Pos{
    int x, y;

    public Pos(int y, int x) {
        this.y = y;
        this.x = x;
    }
}

public class Main {

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

        // System.out.println(new Solution().solution(new String[]{"SOOOL","XXXXO","OOOOO","OXXXX","OOOOE"}));
        System.out.println(new Solution().solution(new String[]{"EOOOO","XXXXX ","LOOSO","XXXXX","OOOOX"}));
    }



}

'PS > programmers' 카테고리의 다른 글

[프로그래머스] - 소수찾기  (0) 2023.02.10
[프로그래머스] - 무인도 여행  (0) 2023.01.27
프로그래머스 - 개인정보 수집 유효기간  (0) 2023.01.18
프로그래머스 - 프렌즈 4블록  (0) 2022.08.30
Programmers - 피로도 (Java) 완전탐색  (0) 2022.08.10
    'PS/programmers' 카테고리의 다른 글
    • [프로그래머스] - 소수찾기
    • [프로그래머스] - 무인도 여행
    • 프로그래머스 - 개인정보 수집 유효기간
    • 프로그래머스 - 프렌즈 4블록
    팡세영
    팡세영
    Android, CS, PS

    티스토리툴바