Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2. bfs = function () {
  3. var queue1 = [];
  4. var queue2 = [];
  5. var queue3 = [];
  6. var queue4 = [];
  7.  
  8. var maze = [
  9. [' ', ' ', ' ', ' '],
  10. [' ', ' ', '*', ' '],
  11. ['*', ' ', '*', ' '],
  12. [' ', ' ', ' ', ' ']
  13. ];
  14.  
  15. let isValid = function (x, y) {
  16. if (x < 0 || y < 0 || x >= bfs.S || y >= bfs.S){
  17. return false; }
  18. else {
  19. return true;
  20. }
  21. };
  22. countPaths = function (maze, x, y, visited, count) {
  23.  
  24. if (x === bfs.S - 1 && y === bfs.S - 1) {
  25. count++;
  26. return count;
  27. }
  28. visited[x][y] = true;
  29. if (isValid(x, y) && maze[x][y] === ' ') {
  30.  
  31. if (x + 1 < bfs.S && !visited[x + 1][y])
  32. count = countPaths(maze, x + 1, y, visited, count);
  33. var index1 = "[" + x + "|" + y + "]";
  34. queue1.push(index1);
  35.  
  36. if (x - 1 >= 0 && !visited[x - 1][y])
  37. count = countPaths(maze, x - 1, y, visited, count);
  38. var index2 = "[" + x + "|" + y + "]";
  39. queue2.push(index2);
  40. if (y + 1 < bfs.S && !visited[x][y + 1])
  41. count = countPaths(maze, x, y + 1, visited, count);
  42. var index3 = "[" + x + "|" + y + "]";
  43. queue3.push(index3);
  44. if (y - 1 >= 0 && !visited[x][y - 1])
  45. count = countPaths(maze, x, y - 1, visited, count);
  46. var index4 = "[" + x + "|" + y + "]";
  47. queue4.push(index4);
  48. }
  49. visited[x][y] = false;
  50. return count;
  51. };
  52. checkPaths = function () {
  53.  
  54. var count = 0;
  55. var visited = (function (dims) {
  56. var allocate = function (dims) { if (dims.length == 0) {
  57. return false;
  58. }
  59. else {
  60. var array = [];
  61. for (var i = 0; i < dims[0]; i++) {
  62. array.push(allocate(dims.slice(1)));
  63. }
  64. return array;
  65. } }; return allocate(dims); })([bfs.S, bfs.S]);
  66. count = countPaths(maze, 0, 0, visited, count);
  67.  
  68.  
  69. function longest_string(str_ara) {
  70. var max = str_ara[0].length;
  71. str_ara.map(v => max = Math.max(max, v.length));
  72. result = str_ara.filter(v => v.length == max);
  73. return result;
  74. }
  75. var paths = [queue1, queue2, queue3, queue4];
  76. var path = longest_string(paths)
  77.  
  78. console.info("There are " + count + " paths" + queue1);
  79. };
  80.  
  81. return countPaths;
  82. }();
  83. bfs.S = 4;
  84.  
  85. checkPaths();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement