Javascript 中的递归不起作用(CodeWars 问题)
Recursion in Javascript not working (CodeWars questions)
我一直在研究一个 Codewars 问题。如果我能让这个递归在我的函数中工作,我就差不多完成了。它应该是深度优先搜索,但在第一次失败后递归调用不会执行。我会指出问题所在。
https://www.codewars.com/kata/path-finder-number-1-can-you-reach-the-exit/train/javascript
function pathFinder(maze) {
maze = maze.split('').filter(e => e !== '\n').join('');
let length = Math.sqrt(maze.length);
console.log('length', length);
let map = maze.split('');
let checked = new Array(maze.length);
checked.fill(false);
let won = [];
helper(0, length, map, checked, won);
return won.length > 0;
}
function helper(pos, length, map, checked, won) {
console.log(pos);
if (pos == map.length - 1) {
won.push(true);
}
checked[pos] = true;
let up = down = left = right = false;
up = checkUp(pos, length, map, checked);
down = checkDown(pos, length, map, checked);
left = checkLeft(pos, length, map, checked);
right = checkRight(pos, length, map, checked);
console.log("U",up,"D",down,"L",left,"R",right);
***************
AT THE FOLLOWING IF STATEMENT, ONCE THE FIRST IF STATEMENT IS TRUE,
THE OTHER ONES DO NOT GET EXECUTED(ex if Up = true, and Down = true,
only Up will get executed)
***************
if (up) {
console.log("U")
helper(pos += length, length, map, checked, won);
}
if (down) {
console.log("D")
helper(pos -= length, length, map, checked, won);
}
if (left) {
console.log("L")
helper(pos -= 1, length, map, checked, won);
}
if (right) {
console.log("R")
helper(pos += 1, length, map, checked, won);
}
}
function checkRow(prev, next, length) {
return Math.floor(prev / length) - Math.floor(next / length);
}
function checkUp(prev, length, arr, checked) {
let next = prev + length;
return (arr.length && (checkRow(prev, next, length) == -1) && arr[next] == '.' && !checked[next]);
}
function checkDown(prev, length, arr, checked) {
let next = prev - length;
return (next >= 0 && (checkRow(prev, next, length) == 1) && arr[next] == '.' && !checked[next]);
}
function checkLeft(prev, length, arr, checked) {
let next = prev - 1;
return (next >= 0 && (checkRow(prev, next, length) == 0) && arr[next] == '.' && !checked[next]);
}
function checkRight(prev, length, arr, checked) {
let next = prev + 1;
return (next <= arr.length && (checkRow(prev, next, length) == 0) && arr[next] == '.' && !checked[next]);
}
你的问题可能是这个:
let up = down = left = right = false;
只初始化一个变量:up
.
只需切换到文件开头带有 "use strict";
的严格模式即可发现问题:
let up = down = left = right = false;
^
ReferenceError: right is not defined
我一直在研究一个 Codewars 问题。如果我能让这个递归在我的函数中工作,我就差不多完成了。它应该是深度优先搜索,但在第一次失败后递归调用不会执行。我会指出问题所在。 https://www.codewars.com/kata/path-finder-number-1-can-you-reach-the-exit/train/javascript
function pathFinder(maze) {
maze = maze.split('').filter(e => e !== '\n').join('');
let length = Math.sqrt(maze.length);
console.log('length', length);
let map = maze.split('');
let checked = new Array(maze.length);
checked.fill(false);
let won = [];
helper(0, length, map, checked, won);
return won.length > 0;
}
function helper(pos, length, map, checked, won) {
console.log(pos);
if (pos == map.length - 1) {
won.push(true);
}
checked[pos] = true;
let up = down = left = right = false;
up = checkUp(pos, length, map, checked);
down = checkDown(pos, length, map, checked);
left = checkLeft(pos, length, map, checked);
right = checkRight(pos, length, map, checked);
console.log("U",up,"D",down,"L",left,"R",right);
***************
AT THE FOLLOWING IF STATEMENT, ONCE THE FIRST IF STATEMENT IS TRUE,
THE OTHER ONES DO NOT GET EXECUTED(ex if Up = true, and Down = true,
only Up will get executed)
***************
if (up) {
console.log("U")
helper(pos += length, length, map, checked, won);
}
if (down) {
console.log("D")
helper(pos -= length, length, map, checked, won);
}
if (left) {
console.log("L")
helper(pos -= 1, length, map, checked, won);
}
if (right) {
console.log("R")
helper(pos += 1, length, map, checked, won);
}
}
function checkRow(prev, next, length) {
return Math.floor(prev / length) - Math.floor(next / length);
}
function checkUp(prev, length, arr, checked) {
let next = prev + length;
return (arr.length && (checkRow(prev, next, length) == -1) && arr[next] == '.' && !checked[next]);
}
function checkDown(prev, length, arr, checked) {
let next = prev - length;
return (next >= 0 && (checkRow(prev, next, length) == 1) && arr[next] == '.' && !checked[next]);
}
function checkLeft(prev, length, arr, checked) {
let next = prev - 1;
return (next >= 0 && (checkRow(prev, next, length) == 0) && arr[next] == '.' && !checked[next]);
}
function checkRight(prev, length, arr, checked) {
let next = prev + 1;
return (next <= arr.length && (checkRow(prev, next, length) == 0) && arr[next] == '.' && !checked[next]);
}
你的问题可能是这个:
let up = down = left = right = false;
只初始化一个变量:up
.
只需切换到文件开头带有 "use strict";
的严格模式即可发现问题:
let up = down = left = right = false; ^
ReferenceError: right is not defined