in Education by
I am trying to implement an algorithm, that finds the shortest path in the following two dimensional array (from the top left corner, to the right bottom corner): [ [ 'A', 'A', 'A', 'B', 'A' ], [ 'B', 'B', 'B', 'B', 'B' ], [ 'A', 'B', 'A', 'A', 'A' ], [ 'A', 'B', 'B', 'B', 'B' ], [ 'A', 'A', 'A', 'A', 'A' ] ] The rules are, that the path must alternate between A's and B's. The output must be a number specifying the smallest number of steps it will take to go through the array. (In the example the expected output is 13) Does anyone know of a simple Graph implementation that can help me solve this problem? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Since it represents an undirected unweighted graph, you can simply use BFS: const m = [ [ 'A', 'A', 'A', 'B', 'A' ], [ 'B', 'B', 'B', 'B', 'B' ], [ 'A', 'B', 'A', 'A', 'A' ], [ 'A', 'B', 'B', 'B', 'B' ], [ 'A', 'A', 'A', 'A', 'A' ] ] let successors = (root, m) => { let connectedCells = [ [root[0] - 1, root[1]], [root[0], root[1] - 1], [root[0] + 1, root[1]], [root[0], root[1] + 1] ] const validCells = connectedCells.filter( (cell) => ( cell[0] >= 0 && cell[0] < m.length && cell[1] >= 0 && cell[1] < m[0].length) ) const successors = validCells.filter( (cell) => (m[cell[0]][cell[1]] !== m[root[0]][root[1]]) ) return successors } const buildPath = (traversalTree, to) => { let path = [to] let parent = traversalTree[to] while (parent) { path.push(parent) parent = traversalTree[parent] } return path.reverse() } const bfs = (from, to) => { let traversalTree = [] let visited = new Set let queue = [] queue.push(from) while (queue.length) { let subtreeRoot = queue.shift() visited.add(subtreeRoot.toString()) if (subtreeRoot.toString() == to.toString()) return buildPath(traversalTree, to) for (child of successors(subtreeRoot, m)) { if (!visited.has(child.toString())){ traversalTree[child] = subtreeRoot queue.push(child) } } } } console.log(bfs([0,0], [4,4]).length) // => 13 Run code snippetExpand snippet

Related questions

0 votes
    I am attempting to solve a problem where if the elements in one array are squared, and all of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I'm trying to assemble and display image slices which have been preloaded into a two dimensional array, but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Consider the following code. What is a good hashing function for the array in Key to be used in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In which search problem, to find the shortest path, each city must be visited once only? Map coloring ... path between a source and a destination Travelling Salesman problem...
asked Mar 8, 2021 in Technology by JackTerrance
0 votes
    I've been working on doing things like Codeacademy, trying to do minor projects on my own like designing ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Series is a one-dimensional labeled array capable of holding any data type. (a) True (b) False The ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    A _________ is a two-dimensional rectangular data set. (a) Vector (b) Lists (c) Matrix (d) Functions I had ... and Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I am trying to use MVC model to create "update news" page. News has an image and i am using ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a table in Db2 called myTable. It has several columns: a | b | date1 | date2 ----- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a table in Db2 called myTable. It has several columns: a | b | date1 | date2 ----- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a table in Db2 called myTable. It has several columns: a | b | date1 | date2 ----- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I store all of the Google Maps marker objects in a single array. Right now I am trying to set up ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I need to filter out any overlaps of start and end (moment.js) times of the following eg array of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
...