Description

ROR game is a game where players are divided into two teams and a team wins if they destroy the camp of the other team first. Therefore, each team's goal is to go to the other team's camp as quickly as possible.

You are going to play the game as a member of one side. Following shows an example where in a 5 x 5 map, your character is placed in (row: 1, column: 1) and camp of the other team is placed in (row: 5, column: 5).

최단거리1_sxuruo.png

In the above figure, you cannot move through black space, while you can move freely through white spaces. The character moves by one space at once in East, West, South, and North directions. Also the character cannot go out of the map.
Following example shows 2 ways to move the character to the other team's camp.

  • In the first method, the character arrives at the camp of the other team through 11 spaces.

최단거리2_hnjd3b.png

  • In the second method, the character arrives at the camp of the other team through 15 spaces.

최단거리3_ntxygd.png

Therefore, the first way is the fastest way to get to the other team's camp.
If the other team builds walls around the camp, you can not move to their camp. For example, in the following case, your character cannot arrive at the other team's camp.

최단거리4_of9xfg.png

Given the status of game map maps as a parameter, write a function solution to return the minimum number of moves required for your character to arrive at the camp of the other team. But, return -1 when there is no way to reach their camp.

Constraints
  • maps is a 2-dimensional array containing the status of the game map with n x m size. n and m are natural number between 1 and 100.
  • n and m may be either the same or different each other, but there is no case where both are given as 1.
  • maps consists of 0 and 1 only, which indicates the space with wall and without wall, respectively.
  • Initially, the character is located at the left top of the game map (1, 1), and the camp of the other team is at the right bottom of the game map (n, m).

Examples
maps answer
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]] 11
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]] -1

Example #1
Data is given as follows.

최단거리6_lgjvrb.png

The shortest path to move your character to the other team's camp is as follows.

최단거리2_hnjd3b (1).png

Therefore, since the character passes through total 11 spaces, return 11.

Example #2
It is the same with an example in the problem statement where there is no way to reach the camp of the other team. Hence return -1.

Result Stop
Result of [Run Test] or [Submit] will be displayed here
내가 제출한 코드가 왜 틀렸는지 프로그래머스 AI에게 물어보세요.
제출 후 채점하기를 눌러 30점 이상인 경우 물어볼 수 있어요.
베타 기간 동안에는 한 문제당 1번만 물어볼 수 있어요.