Amazing Mazes

Interview

Time limit1sMemory limit128 MB

Summary
Given a rectangular grid with walls between cells, find the number of cells on the shortest path from the top-left opening to the bottom-right opening, or 0 if none exists.
Level

Medium4 of 10

Topics
BFS, Graph, Matrix, Implementation
Solved
No attempts yet

Problem

You are asked to solve maze problems. If you cannot get through these mazes, you might not get through the contest either!

A maze is a rectangular area filled with unit squares arranged in rows and columns. The area is enclosed by walls except for its entry and exit. The entry is at the leftmost part of the top side of the rectangle: the top edge of the top-left square is open. The exit is at the rightmost part of the bottom side in the same way: the bottom edge of the bottom-right square is open.

Inside the maze you may move from a square to any square that is horizontally or vertically adjacent to it. Two adjacent squares may be separated by a wall, however, and you cannot pass through a wall.

Your task is to find the length of the shortest path from the entry to the exit. There may be more than one shortest path, or there may be none.

Input

The input consists of one or more datasets, each describing one maze.

The first line of a dataset contains two integers, the width ww and the height hh of the rectangular area, in this order.

The next 2h−12h - 1 lines describe whether walls separate the squares.

  • The odd-numbered lines (the 1st, 3rd, … of these lines) each begin with a single space followed by w−1w - 1 integers separated by spaces. The kk-th integer says whether a wall stands between the kk-th and (k+1)(k+1)-th squares (counting from the left) of one row. Read from top to bottom, these lines correspond to row 11, row 22, …, row hh.
  • The even-numbered lines (the 2nd, 4th, …) each contain ww integers separated by spaces. The kk-th integer says whether a wall stands between the kk-th squares of two vertically adjacent rows.

An integer 11 means a wall is present; 00 means no wall is there.

The end of the input is indicated by a line containing two zeros.

The number of datasets is at most 100100. Both the width and the height are between 22 and 3030, inclusive.

Output

For each dataset, output on a single line one integer: the length of the shortest path from the entry to the exit, measured as the number of squares visited (the entry and exit squares are both counted). If there is no path through the maze, output 00. The line must contain nothing but this number.

Examples1

  1. Example 1

    Input
    2 3
     1
    0 1
     0
    1 0
     1
    9 4
     1 0 1 0 0 0 0 0
    0 1 1 0 1 1 0 0 0
     1 0 1 1 0 0 0 0
    0 0 0 0 0 0 0 1 1
     0 0 0 1 0 0 1 1
    0 0 0 0 1 1 0 0 0
     0 0 0 0 0 0 1 0
    12 5
     1 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 1 0 0 0 0 0 0 0
     1 0 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 1 0 0 0 0
     0 0 1 0 0 1 0 1 0 0 0
    0 0 0 1 1 0 1 1 0 1 1 0
     0 0 0 0 0 1 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 1 0 0
    0 0
    
    Expected output
    4
    0
    20