Finding an Exit from a Maze using undirected graphs.

We can think of a maze as a rectangular grid of cells with paths between adjacent cells. If we want to find if there is a path from a given cell to a given exit from the maze, where the exit is represented by a cell, you can represent the maze as an undirected graph.

The nodes of the graph are cells of the maze, and two nodes are connected an undirected edge if they are adjacent and there is no wall between them. Therefore we can surmise we just need to see if a path, series of edges connecting the nodes, to determine if the two nodes are connected.

See code below.

Problem: undirected graph and two distinct vertices u and v, check if there is a path between u and v.

Input Format: An undirected graph with n vertices and m edges. The next line contains two vertices u and v of the graph.

Constraints: 2 ≤ n ≤ 10 3 ; 1 ≤ m ≤ 10 3 ; 1 ≤ u, v ≤ n; u != v.

Result: 1 if path exists, 0 if no path exists.