Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var maze = [
  4. ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
  5. ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
  6. ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
  7. ['*', '*', '*', '*', 0 , ' ', ' ', '*', '*', '*', '*', '*'],
  8. ['*', '*', '*', '*', ' ', '*', '*', '*', '*', '*', ' ', ' '],
  9. ['*', '*', '*', '*', ' ', '*', '*', '*', '*', '*', ' ', '*'],
  10. ['*', '*', '*', '*', ' ', '*', '*', '*', '*', '*', ' ', '*'],
  11. ['*', '*', '*', '*', ' ', '*', '*', '*', ' ', ' ', ' ', '*'],
  12. [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', '*', '*'],
  13. ['*', ' ', '*', '*', '*', '*', '*', '*', ' ', '*', '*', '*'],
  14. ['*', ' ', '*', '*', '*', '*', '*', '*', ' ', '*', '*', '*'],
  15. ['*', ' ', ' ', ' ', ' ', '*', '*', '*', ' ', '*', '*', '*'],
  16. ['*', '*', '*', '*', ' ', '*', '*', '*', ' ', '*', '*', '*'],
  17. ['*', '*', '*', '*', ' ', '*', '*', '*', ' ', ' ', ' ', '*'],
  18. ['*', '*', '*', '*', ' ', '*', '*', '*', '*', '*', ' ', '*']
  19. ];
  20.  
  21. var queue;
  22. var exit_found;
  23.  
  24. solve_maze(maze);
  25.  
  26. function solve_maze(maze) {
  27. exit_found = false;
  28. queue = find_start(maze)
  29. while(queue.length != 0) {
  30. let position = queue.shift();
  31.  
  32. try_move_position({row:position.row+1, column:position.column, path:position.path});
  33. try_move_position({row:position.row-1, column:position.column, path:position.path});
  34. try_move_position({row:position.row, column:position.column+1, path:position.path});
  35. try_move_position({row:position.row, column:position.column-1, path:position.path});
  36. }
  37. if(!exit_found) {
  38. console.log("No exit found.");
  39. }
  40. }
  41.  
  42. function try_move_position(position) {
  43. if(!is_inside_maze(position)) {
  44. exit_found = true;
  45. print_path(position);
  46. return;
  47. }
  48. if(maze[position.row][position.column] === ' ') {
  49. let new_path = position.path.slice();
  50. new_path.push({row:position.row, column:position.column});
  51. queue.push({row:position.row, column:position.column, path:new_path});
  52. maze[position.row][position.column] = 'x';
  53. }
  54. }
  55.  
  56. function is_inside_maze(position) {
  57. if(position.row >= 0 && position.row < maze.length &&
  58. position.column >= 0 && position.column < maze[0].length)
  59. return true;
  60. }
  61.  
  62. function print_path(position) {
  63. let path = position.path;
  64. path.forEach(step => {
  65. console.log(step);
  66. });
  67. console.log("exit");
  68. }
  69.  
  70. function find_start(maze) {
  71. let start = {row: 0, column: 0, path: []};
  72. for (let row = 0; row < maze.length; row++) {
  73. for (let column = 0; column < maze[0].length; column++) {
  74. if(maze[row][column] === 0) {
  75. start.row = row;
  76. start.column = column;
  77. }
  78. }
  79. }
  80. return [start];
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement