ITLabAssign 3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Assignment-3 (Due 15th January 2021)

A number maze is an nXn grid of positive integers. A token starts in the upper left
corner; your goal is to move the token to the lower-right corner. On each turn, you are
allowed to move the token up, down, left, or right; the distance you may move the
token is determined by the number on its current square. For example, if the token is
on a square labeled3, then you may move the token three steps up, three steps down,
three steps left, o rthree steps right. However, you are never allowed to move the
token off the edge of the board. For example, given the number maze in Figure., your
algorithm should return the integer 8.

Write a program that asks for an input file that contains the input number maze M , and
outputs the minimum number of moves required to solve the maze; if the maze does not
admit a solution, your program should output that. There is no need to output the actual
sequence of moves. For full credit, your program should be able to correctly solve inputs of
length n = 1000 reasonably fast, say within a second to two minutes. (Input files will be
uploaded)
We can convert the number maze M into graph and use BFS on this graph to find a solution.
First, we make each number in the number maze a vertex. Thus there are a total of n 2 vertices
corresponding to an n×n maze. Then for each vertex Mi,j, we read its value k in the number
maze and connect Mi,j to Mi+k,j, Mi−k,j, Mi,j+k, Mi,j−k (provided these vertices exist).
Once the graph is completely constructed, we can use BFS to find a shortest path from the
vertex corresponding to the top-left square to the vertex corresponding to the bottom-right
square.

You might also like