Lost On Campus

면접 대비

시간 제한1초메모리 제한2048 MB

요약
벽, 문, 출구, 시작점으로 이루어진 격자 지도에서 출구에 도달할 때 지나야 하는 문의 최소 개수를 구한다.
난이도

보통10점 중 5점

유형
그래프, BFS, 최단 경로, 행렬
정답자
아직 제출이 없습니다

문제

You were wandering around the campus at Colorado School of Mines, but you ended up getting lost in some building that you don't recognize. To get out, you'll need to traverse the corridors of the building until you find an exit.

Luckily, while wandering, you're able to find a map of the building that shows you the floor plan (your input). Now you can find a way out!

There's just one problem, however: you have a tragic fear of doors. On the way out of the building, you'll want to go through as few doors as possible.

Given the floor plan of the building, what is the fewest doors you can go through while still reaching an exit?

입력

The first line of your input will contain two integers: WW and HH, representing the width and height, respectively, of the map. For all inputs, 3≤W≤1003 \leq W \leq 100 and 3≤H≤1003 \leq H \leq 100.

The following HH lines will each contain WW characters, representing the map. The map itself uses the following characters to represent each object:

  • . (period): Floor
  • # (pound sign): Wall
  • D (upper-case D): Door
  • E (upper-case E): Exit
  • * (asterisk): Starting Point (You are here!)

When traversing the map, you can only move from a given tile to its four adjacent tiles: up, down, left, and right.

Additionally, each map will have walls along every edge, and there can be several different exits in the map, which may or may not be adjacent to the edges of the map.

출력

Your output should be a single integer, representing the fewest doors you can go through while still reaching an exit. Also note that going through an exit does not count as going through a door.

If it is impossible to navigate to an exit, output "NOT POSSIBLE".

예제3

  1. 예제 1

    입력
    9 12
    #########
    #E..D.###
    #...#.###
    #####.###
    #.....###
    #D#.#D###
    #D#D#.#E#
    #D#D#.#.#
    #.....#D#
    #..*..DD#
    #.....###
    #########
    
    예상 출력
    2
    
  2. 예제 2

    입력
    9 10
    #########
    #...E..D#
    #########
    #.D....D#
    ###.*.###
    ###D.####
    ####.####
    #E.###..#
    #.......#
    #########
    
    예상 출력
    NOT POSSIBLE
    
  3. 예제 3

    입력
    8 8
    ########
    ###....#
    ###E.D.#
    #.*##D##
    #..##D##
    #..DD.##
    #..#####
    ########
    
    예상 출력
    5