我一直在研究一种寻路算法,在很大程度上我了解它,但仍然让我困惑的是如何设置正确的路径。
它打印出来了
[ 'East' ]
[ 'South' ]
[ 'East', 'East' ]
[ 'South', 'South' ]
[ 'East', 'East', 'East' ]
[ 'South', 'South', 'South' ]
[ 'South', 'South', 'South', 'East' ]
[ 'South', 'South', 'South', 'East', 'East' ]
[ 'South', 'South', 'South', 'East', 'East', 'North' ]
它在代码中的何处/如何决定,嘿['East'、'East'、'East']不再工作并从路径数组中丢弃它?
var findShortestPath = function(startCoordinates, grid) {
var distanceFromTop = startCoordinates[0];
var distanceFromLeft = startCoordinates[1];
var location = {
distanceFromTop: distanceFromTop,
distanceFromLeft: distanceFromLeft,
path: [],
status: 'Start'
};
var queue = [location];
while (queue.length > 0) {
var currentLocation = queue.shift();
var newLocation = exploreInDirection(currentLocation, 'North', grid);
if (newLocation.status === 'Goal') {
return newLocation.path;
} else if (newLocation.status === 'Valid') {
queue.push(newLocation);
}
var newLocation = exploreInDirection(currentLocation, 'East', grid);
if (newLocation.status === 'Goal') {
return newLocation.path;
} else if (newLocation.status === 'Valid') {
queue.push(newLocation);
}
var newLocation = exploreInDirection(currentLocation, 'South', grid);
if (newLocation.status === 'Goal') {
return newLocation.path;
} else if (newLocation.status === 'Valid') {
queue.push(newLocation);
}
var newLocation = exploreInDirection(currentLocation, 'West', grid);
if (newLocation.status === 'Goal') {
return newLocation.path;
} else if (newLocation.status === 'Valid') {
queue.push(newLocation);
}
}
return false;
};
var locationStatus = function(location, grid) {
var gridSize = grid.length;
var dft = location.distanceFromTop;
var dfl = location.distanceFromLeft;
if (location.distanceFromLeft < 0 ||
location.distanceFromLeft >= gridSize ||
location.distanceFromTop < 0 ||
location.distanceFromTop >= gridSize) {
return 'Invalid';
} else if (grid[dft][dfl] === 'Goal') {
return 'Goal';
} else if (grid[dft][dfl] !== 'Empty') {
return 'Blocked';
} else {
return 'Valid';
}
};
var exploreInDirection = function(currentLocation, direction, grid) {
var newPath = currentLocation.path.slice();
newPath.push(direction);
var dft = currentLocation.distanceFromTop;
var dfl = currentLocation.distanceFromLeft;
if (direction === 'North') {
dft -= 1;
} else if (direction === 'East') {
dfl += 1;
} else if (direction === 'South') {
dft += 1;
} else if (direction === 'West') {
dfl -= 1;
}
var newLocation = {
distanceFromTop: dft,
distanceFromLeft: dfl,
path: newPath,
status: 'Unknown'
};
newLocation.status = locationStatus(newLocation, grid);
if (newLocation.status === 'Valid') {
grid[newLocation.distanceFromTop][newLocation.distanceFromLeft] = 'Visited';
}
return newLocation;
};
var gridSize = 4;
var grid = [];
for (var i=0; i<gridSize; i++) {
grid[i] = [];
for (var j=0; j<gridSize; j++) {
grid[i][j] = 'Empty';
}
}
grid[0][0] = "Start";
grid[2][2] = "Goal";
grid[1][1] = "Obstacle";
grid[1][2] = "Obstacle";
grid[1][3] = "Obstacle";
grid[2][1] = "Obstacle";
console.log(findShortestPath([0,0], grid));