아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

The Grove

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

요약
나무 숲이 하나 있는 격자에서 8방향 이동으로 숲을 한 바퀴 도는 닫힌 경로를 찾고 최소 걸음 수를 구한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 기하
정답자
아직 제출이 없습니다

문제

The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take.

Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 ≤ R ≤ 50, 1 ≤ C ≤ 50). Here's a typical example where '.' is pasture (which Bessie may traverse), 'X' is the grove of trees, '*' represents Bessie's start and end position, and '+' marks one shortest path she can walk to circumnavigate the grove (i.e., the answer):

...+...
..+X+..
.+XXX+.
..+XXX+
..+X..+
...+++*

The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path.

입력

  • Line 1: Two space-separated integers: R and C
  • Lines 2..R+1: Line i+1 describes row i with C characters (with no spaces between them).

출력

  • Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.

예제1

  1. 예제 1

    입력
    6 7
    .......
    ...X...
    ..XXX..
    ...XXX.
    ...X...
    ......*
    
    예상 출력
    13