Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/fda3ab78b24f704cf2f1c15ab5567353 to your computer and use it in GitHub Desktop.
Save jianminchen/fda3ab78b24f704cf2f1c15ab5567353 to your computer and use it in GitHub Desktop.
maze - using recursive, depth first search - it took Julia over 10 minutes to set line 46 - mark as visited, she ran into stack overflow issues.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MazeProject
{
/// <summary>
/// code review - July 18, 2017
/// Maze -
/// Given an array, there is only one item is value of 9, all others are 0 or 1.
/// 0 stands for the exit is not availabe, 1 is ok to continue. Make a judgement
/// if starting from (0,0), 4 directions: upper, down, left and right, and see
/// if there is path to find 9.
/// </summary>
class Program
{
static void Main(string[] args)
{
var maze = new int[3][]{
new int[]{1, 0, 0, 1},
new int[]{1, 1, 1, 1},
new int[]{1, 0, 0, 9}};
Console.WriteLine(checkMaze(maze, 0, 0));
}
public static bool checkMaze(int[][] maze, int row, int col)
{
if (maze == null || maze.Length == 0 || maze[0].Length == 0 ||
row < 0 || row >= maze.Length ||
col < 0 || col >= maze[0].Length ||
maze[row][col] == 0 )
{
return false;
}
if (maze[row][col] == 9)
{
return true;
}
// mark as visited, Julia forget to set it, ran into stack overflow.
// It took her 10 minute to figure out the issue.
maze[row][col] = 0;
// depth first search, go over each recursive branch if need.
return checkMaze(maze, row + 1, col) ||
checkMaze(maze, row - 1, col) ||
checkMaze(maze, row, col + 1) ||
checkMaze(maze, row, col - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment