Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bfs = function () {
- var queue1 = [];
- var queue2 = [];
- var queue3 = [];
- var queue4 = [];
- var maze = [
- [' ', ' ', ' ', ' '],
- [' ', ' ', '*', ' '],
- ['*', ' ', '*', ' '],
- [' ', ' ', ' ', ' ']
- ];
- let isValid = function (x, y) {
- if (x < 0 || y < 0 || x >= bfs.S || y >= bfs.S){
- return false; }
- else {
- return true;
- }
- };
- countPaths = function (maze, x, y, visited, count) {
- if (x === bfs.S - 1 && y === bfs.S - 1) {
- count++;
- return count;
- }
- visited[x][y] = true;
- if (isValid(x, y) && maze[x][y] === ' ') {
- if (x + 1 < bfs.S && !visited[x + 1][y])
- count = countPaths(maze, x + 1, y, visited, count);
- var index1 = "[" + x + "|" + y + "]";
- queue1.push(index1);
- if (x - 1 >= 0 && !visited[x - 1][y])
- count = countPaths(maze, x - 1, y, visited, count);
- var index2 = "[" + x + "|" + y + "]";
- queue2.push(index2);
- if (y + 1 < bfs.S && !visited[x][y + 1])
- count = countPaths(maze, x, y + 1, visited, count);
- var index3 = "[" + x + "|" + y + "]";
- queue3.push(index3);
- if (y - 1 >= 0 && !visited[x][y - 1])
- count = countPaths(maze, x, y - 1, visited, count);
- var index4 = "[" + x + "|" + y + "]";
- queue4.push(index4);
- }
- visited[x][y] = false;
- return count;
- };
- checkPaths = function () {
- var count = 0;
- var visited = (function (dims) {
- var allocate = function (dims) { if (dims.length == 0) {
- return false;
- }
- else {
- var array = [];
- for (var i = 0; i < dims[0]; i++) {
- array.push(allocate(dims.slice(1)));
- }
- return array;
- } }; return allocate(dims); })([bfs.S, bfs.S]);
- count = countPaths(maze, 0, 0, visited, count);
- function longest_string(str_ara) {
- var max = str_ara[0].length;
- str_ara.map(v => max = Math.max(max, v.length));
- result = str_ara.filter(v => v.length == max);
- return result;
- }
- var paths = [queue1, queue2, queue3, queue4];
- var path = longest_string(paths)
- console.info("There are " + count + " paths" + queue1);
- };
- return countPaths;
- }();
- bfs.S = 4;
- checkPaths();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement