Skip to content

Commit

Permalink
Add dfs maze search (keon#613)
Browse files Browse the repository at this point in the history
* [Add] Create maze_search.py

Add maze_search.py in dfs section.

* [Edit] Add description and examples

Add description and examples in maze_search problem.

* [Edit] Add methods in maze_search

Add find_path and dfs methods in dfs_maze_search problem.

* [Edit] Add global variable

Add global variable directions in maze_search problem in dfs section.

* [Edit] Add global variable

Add global variable cnt in maze_search problem in dfs section.

* [Edit] Add return condition

Add return condition of dfs method.

* [Edit] Add code in dfs method

Move i and j following defined directions.

* [Edit] Add code in dfs method

Implement recursive call in dfs method.

* [Fix] Implement dfs method

Fix wrong variable names in dfs method.

* [Edit] Add dfs method call and testing code

Complete find_path method and add testing code.

* [Edit] Delete testing code

* [Edit] Delete global variable

Change global variable directions to local variable.

* [Edit] Delete global variable

Change global variable cnt to local variable.

* [Edit] Add description

Add description in maze_search problem in dfs section.

* [Edit] Add unittest

Implement unittest func. of maze_search problem and edit init file.

* [Edit] Edit unittest

Change wrong test answer.
  • Loading branch information
guswh11 authored and keon committed Dec 13, 2019
1 parent 595d107 commit 63fa0a2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions algorithms/dfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .pacific_atlantic import *
from .sudoku_solver import *
from .walls_and_gates import *
from .maze_search import *
55 changes: 55 additions & 0 deletions algorithms/dfs/maze_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'''
Find shortest path from top left column to the right lowest column using DFS.
only step on the columns whose value is 1
if there is no path, it returns -1
(The first column(top left column) is not included in the answer.)
Ex 1)
If maze is
[[1,0,1,1,1,1],
[1,0,1,0,1,0],
[1,0,1,0,1,1],
[1,1,1,0,1,1]],
the answer is: 14
Ex 2)
If maze is
[[1,0,0],
[0,1,1],
[0,1,1]],
the answer is: -1
'''


def find_path(maze):
cnt = dfs(maze, 0, 0, 0, -1)
return cnt


def dfs(maze, i, j, depth, cnt):
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]

row = len(maze)
col = len(maze[0])

if i == row - 1 and j == col - 1:
if cnt == -1:
cnt = depth
else:
if cnt > depth:
cnt = depth
return cnt

maze[i][j] = 0

for k in range(len(directions)):
nx_i = i + directions[k][0]
nx_j = j + directions[k][1]

if nx_i >= 0 and nx_i < row and nx_j >= 0 and nx_j < col:
if maze[nx_i][nx_j] == 1:
cnt = dfs(maze, nx_i, nx_j, depth + 1, cnt)

maze[i][j] = 1

return cnt
11 changes: 10 additions & 1 deletion tests/test_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
num_islands,
pacific_atlantic,
Sudoku,
walls_and_gates
walls_and_gates,
find_path
)

import unittest
Expand Down Expand Up @@ -50,6 +51,14 @@ def test_walls_and_gates(self):
walls_and_gates(rooms)
self.assertEqual([[3, -1, 0, 1], [2, 2, 1, -1], [1, -1, 2, -1], [0, -1, 3, 4]], rooms)

class TestMazeSearch(unittest.TestCase):
def test_maze_search(self):
maze_1 = [[1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1], [1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1]]
self.assertEqual(37, find_path(maze_1))
maze_2 = [[1,0,1,1,1,1], [1,0,1,0,1,0], [1,0,1,0,1,1], [1,1,1,0,1,1]]
self.assertEqual(14, find_path(maze_2))
maze_3 = [[1,0,0], [0,1,1], [0,1,1]]
self.assertEqual(-1, find_path(maze_3))

if __name__ == "__main__":
unittest.main()

0 comments on commit 63fa0a2

Please sign in to comment.