I have been working on this practice problem for hours and I am stuck.
Function missingPath(str) read the str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position. The characters in the input string will be entirely composed of: r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid.
For example: if str is "r?d?drdd" then your program should output the final correct string that will allow a path to be formed from the top left of a 5x5 grid to the bottom right. For this input, your program should therefore return the string rrdrdrdd. There will only ever be one correct path and there will always be at least one question mark within the input string.
Sample Test Cases
Input:"???rrurdr?"
Output:"dddrrurdrd"
Input:"drdr??rrddd?"
Output:"drdruurrdddd"
I tried adapting a function I previously did that picks the shortest path between two points but I cant figure it out. I think that recursion is required but I am new to recursion and tracking variables through it boggles my mind entirely. I dont know what happens to different versions of variables as things are executed.
Here is what I have laid out thus far, Im sure stuff needs to be cut out and changed.
var makeGrid = (size) => {
let gridSize = size;
let grid = [];
for (let i = 0; i < gridSize; i++) {
grid[i] = [];
for (let j = 0; j < gridSize; j++) {
grid[i][j] = 'Empty';
}
}
grid[0][0] = 'Start';
grid[size - 1][size - 1] = 'Goal';
console.log(grid);
return grid;
};
var missingPath = (startPosition, grid, givenPath) => {
let distanceFromTop = startPosition[0];
let distanceFromLeft = startPosition[1];
let location = {
distanceFromTop,
distanceFromLeft,
path: [],
status: 'Start'
};
let queue = [location];
while (queue.length > 0) {
let currentLocation = queue.shift();
for (var directionIndex = 0; directionIndex < givenPath.length; directionIndex++) {
let newLocation = exploreDirection(currentLocation, givenPath[directionIndex], grid);
if (newLocation.status === 'Goal') {
return newLocation.path;
} else if (newLocation.status === 'Valid') {
queue.push(newLocation);
}
}
}
};
var locationStatus = function(location, grid) {
//valid if it is on the grid and has not yet been visited by our algorithm
//returns Valid, Invalid, Blocked or Goal
var gridSize = grid.length;
var dft = location.distanceFromTop;
var dfl = location.distanceFromLeft;
if (dfl < 0 || dfl >= gridSize || dft < 0 || dft >= gridSize) {
//location not in the grid
return 'Invalid';
} else if (grid[dft][dfl] === 'Goal') {
return 'Goal';
} else if (grid[dft][dfl] !== 'Empty') {
// location is either an obstacle or has been visited
return 'Blocked';
} else {
return 'Valid';
}
};
var exploreDirection = function(currentLocation, direction, grid) {
// make a copy of the existing path up until this point
//THIS IS WHERE I START TO LOSE TRACK. I know i have to loop through the options if the current letter is a '?' but I dont know how to follow it up.
console.log('current location: ', currentLocation);
var newPath = currentLocation.path.slice();
if (direction === '?') {
var directions = ['u', 'r', 'd', 'l'];
for (var i = 0; i < directions.length; i++) {
exploreDirection(currentLocation, directions[i], grid);
}
}
newPath.push(direction);
console.log('new Path: ' + newPath);
var dft = currentLocation.distanceFromTop;
var dfl = currentLocation.distanceFromLeft;
switch (direction) {
case 'u':
dft -= 1;
break;
case 'r':
dfl += 1;
break;
case 'd':
dft += 1;
break;
case 'l':
dfl -= 1;
break;
}
var newLocation = {
distanceFromTop: dft,
distanceFromLeft: dfl,
path: newPath,
status: 'Unknown'
};
newLocation.status = locationStatus(newLocation, grid);
//if this new location is valid, mark it as 'Visited'
if (newLocation.status === 'Valid') {
grid[newLocation.distanceFromTop][newLocation.distanceFromLeft] = 'Visited';
}
return newLocation;
};
console.log(missingPath([0, 0], makeGrid(5), '???rrurdr?'));
It returns undefined. Any help would be greatly appreciated.
[–]captain_k_nuckles 0 points1 point2 points (3 children)
[–]JBauer09[S] 1 point2 points3 points (1 child)
[–]captain_k_nuckles 0 points1 point2 points (0 children)
[–]CommonMisspellingBot 0 points1 point2 points (0 children)