-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2625bfb
commit d2bdcdb
Showing
21 changed files
with
453 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export function dijkstra(grid, startNode, finishNode) { | ||
const visitedNodesInOrder = []; | ||
startNode.distance = 0; | ||
const unvisitedNodes = getAllNodes(grid); | ||
while (!!unvisitedNodes.length) { | ||
sortNodesByDistance(unvisitedNodes); | ||
const closestNode = unvisitedNodes.shift(); | ||
// If we encounter a wall, we skip it. | ||
if (closestNode.isWall) continue; | ||
// If the closest node is at a distance of infinity, | ||
// we must be trapped and should therefore stop. | ||
if (closestNode.distance === Infinity) return visitedNodesInOrder; | ||
closestNode.isVisited = true; | ||
visitedNodesInOrder.push(closestNode); | ||
if (closestNode === finishNode) return visitedNodesInOrder; | ||
updateUnvisitedNeighbors(closestNode, grid); | ||
} | ||
} | ||
|
||
function sortNodesByDistance(unvisitedNodes) { | ||
unvisitedNodes.sort((nodeA, nodeB) => nodeA.distance - nodeB.distance); | ||
} | ||
|
||
function updateUnvisitedNeighbors(node, grid) { | ||
const unvisitedNeighbors = getUnvisitedNeighbors(node, grid); | ||
for (const neighbor of unvisitedNeighbors) { | ||
neighbor.distance = node.distance + 1; | ||
neighbor.previousNode = node; | ||
} | ||
} | ||
|
||
function getUnvisitedNeighbors(node, grid) { | ||
const neighbors = []; | ||
const {col, row} = node; | ||
if (row > 0) neighbors.push(grid[row - 1][col]); | ||
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]); | ||
if (col > 0) neighbors.push(grid[row][col - 1]); | ||
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]); | ||
return neighbors.filter(neighbor => !neighbor.isVisited); | ||
} | ||
|
||
function getAllNodes(grid) { | ||
const nodes = []; | ||
for (const row of grid) { | ||
for (const node of row) { | ||
nodes.push(node); | ||
} | ||
} | ||
return nodes; | ||
} | ||
|
||
// Backtracks from the finishNode to find the shortest path. | ||
// Only works when called *after* the dijkstra method above. | ||
export function getNodesInShortestPathOrder(finishNode) { | ||
const nodesInShortestPathOrder = []; | ||
let currentNode = finishNode; | ||
while (currentNode !== null) { | ||
nodesInShortestPathOrder.unshift(currentNode); | ||
currentNode = currentNode.previousNode; | ||
} | ||
return nodesInShortestPathOrder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +0,0 @@ | ||
.App { | ||
text-align: center; | ||
} | ||
|
||
.App-logo { | ||
height: 40vmin; | ||
pointer-events: none; | ||
} | ||
|
||
@media (prefers-reduced-motion: no-preference) { | ||
.App-logo { | ||
animation: App-logo-spin infinite 20s linear; | ||
} | ||
} | ||
|
||
.App-header { | ||
background-color: #282c34; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
} | ||
|
||
.App-link { | ||
color: #61dafb; | ||
} | ||
|
||
@keyframes App-logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.