The Maze Runner

0

0 votes
Graph, Algorithms, Breadth-first search, Graphs
Problem

You bought two virtual reality glasses. There is only one game installed to both of them called "The Maze Runner".

Each glasses has its special feature.

  1. The first glasses, allows you to pass through doors.
  2. The second glasses, allows you to walk through walls.

The maze looks like a gird of size N×M. At first, you are at the top left corner of this grid. You want to reach the bottom right corner of this maze. You can move vertically or horizontally on the grid and need 1 minute to go from one cell to another. The cells with obstacles cannot be traversed, obstacles are represented in doors and walls. Each type of glasses allows passing one type of obstacle but not the other one.

How long do you need to reach the bottom right corner using the first glasses and second glasses?

Given the grid which represents the maze. Print the minimum time needed to reach the bottom right corner using the first and second glasses. If you can't reach the bottom right corner using either glasses, print -1.

Example

Let's assume N = 4 and M = 5, and the grid is as follows

. . . . .
* * . * *
. # # . #
. . . . .

If you use the first glass you can reach the bottom right corner following (1,1)(1,2)(1,3)(2,3)(3,3)(4,3)(4,4)(4,5). ie by using 7 steps.

If you use the second glass can reach the bottom right corner following  (1,1)(1,2)(1,3)(1,4)(2,4)(3,4)(4,4)(4,5) ie by using 7 steps.

Function description

Complete the solve function provided in the editor. This function takes the following 2 parameters and returns an integer array of length 2 containing minimum time required by each glasses.

  • N: Represents an integer denoting the number of rows in the grid.

  • M: Represents an integer denoting the number of columns in the grid.

  • maze: Represents a string array of size , in which each string denotes each row of the maze.

Input format

  • The first line contains two integers, NM denoting the number of rows and columns in the grid.
  • The following rows lines will have strings of the same length representing the maze. Each character in maze[i] will be either a '.' representing a clear passage or an '*' representing a wall or an '#' representing a door.

Output format

Two lines contain two integers, the first one denotes the minimum time you need to reach the bottom right corner with the first glasses, the second entity denotes the minimum time you need to reach the destination using the second glasses. If you can't reach the bottom right corner using either glasses, print -1.

Code Snippets

Code snippets are written in languages C, C++, Java, Python.

Constraints

  • 1 ≤ N, M ≤ 500
  • It is guaranteed that all values of the maze consist of '.' for paths, '*' for walls, and '#' for doors.
  • The top left and bottom right corners of the maze will not be obstacles.

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In this sample, you cannot find your way to the bottom right corner with the first glasses. So, the first entity of the answer is -1.

You can reach the bottom right corner in 8 minutes with the second glasses.

Editor Image

?