Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the BFS algorithm in JavaScript #10

Open
nakov opened this issue Nov 11, 2019 · 0 comments
Open

Implement the BFS algorithm in JavaScript #10

nakov opened this issue Nov 11, 2019 · 0 comments

Comments

@nakov
Copy link
Contributor

nakov commented Nov 11, 2019

Implement in JavaScript the BFS algorithm (Breadth First Search) to find the shortest path to exit from a labyrinth, starting from certain cell.

Print the shortest path as sequence of adjacent cells from start to the exit. If several shortest paths exist, print one of them. If no path goes outside of the labyrinth, print "No path to exit the labyrinth".

Sample labyrinth (maze) definition in JS:

let maze = 
    [
		['*', '*', '*', ' ', '*', '*', '*'],
		['*', 's', '*', ' ', ' ', '*', '*'],
		['*', ' ', '*', '*', ' ', ' ', ' '],
		['*', ' ', ' ', '*', ' ', '*', '*'],
		['*', ' ', ' ', ' ', ' ', '*', '*'],
		['*', '*', '*', ' ', '*', '*', '*'],
		['*', ' ', ' ', ' ', '*', '*', '*'],
		['*', ' ', '*', '*', '*', '*', '*'],
		['*', ' ', '*', '*', '*', '*', '*'],
    ];

Sample output from your solution for the above example:

Shortest path: 
  step 0 (start): { row: 1, col: 1 } ->
  step 1: { row: 2, col: 1 } ->
  step 2: { row: 3, col: 1 } ->
  step 3: { row: 4, col: 1 } ->
  step 4: { row: 4, col: 2 } ->
  step 5: { row: 4, col: 3 }  ->
  step 6: { row: 4, col: 4 } ->
  step 7: { row: 3, col: 4 } ->
  step 8: { row: 2, col: 4 } ->
  step 9: { row: 2, col: 5 } ->
  step 10: { row: 2, col: 6 } ->
  step 11 (exit): { row: 2, col: 7 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant