Try A star algorithm
This commit is contained in:
109
2024/18/index.ts
109
2024/18/index.ts
@@ -16,6 +16,8 @@ enum Dir {
|
|||||||
|
|
||||||
const map: Array<Array<string>> = [];
|
const map: Array<Array<string>> = [];
|
||||||
|
|
||||||
|
const path: Array<Coord> = [];
|
||||||
|
|
||||||
const width = 71;
|
const width = 71;
|
||||||
const height = 71;
|
const height = 71;
|
||||||
|
|
||||||
@@ -112,20 +114,108 @@ function fillDeadEnd(pos: Coord): void {
|
|||||||
}
|
}
|
||||||
printMap();
|
printMap();
|
||||||
|
|
||||||
for (let y = 0; y < height; y++) {
|
// for (let y = 0; y < height; y++) {
|
||||||
for (let x = 0; x < width; x++) {
|
// for (let x = 0; x < width; x++) {
|
||||||
const pos = { x, y };
|
// const pos = { x, y };
|
||||||
const char = get(pos);
|
// const char = get(pos);
|
||||||
if (char !== '.') {
|
// if (char !== '.') {
|
||||||
continue;
|
// continue;
|
||||||
|
// }
|
||||||
|
// if (isDeadEnd(pos)) {
|
||||||
|
// fillDeadEnd(pos);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// printMap();
|
||||||
|
|
||||||
|
interface Nd {
|
||||||
|
parent: Nd|null,
|
||||||
|
pos: Coord,
|
||||||
|
g: number,
|
||||||
|
h: number,
|
||||||
|
f: number,
|
||||||
|
};
|
||||||
|
|
||||||
|
const openList: Array<Nd> = [];
|
||||||
|
const closedList: Array<Nd> = [];
|
||||||
|
|
||||||
|
const startNode: Nd = {
|
||||||
|
parent: null,
|
||||||
|
pos: { x: 0, y: 0 },
|
||||||
|
g: 0,
|
||||||
|
h: 0,
|
||||||
|
f: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const endNode = {
|
||||||
|
parent: null,
|
||||||
|
pos: { x: width - 1, y: height - 1 },
|
||||||
|
g: 0,
|
||||||
|
h: 0,
|
||||||
|
f: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
openList.push(startNode);
|
||||||
|
|
||||||
|
while (openList.length > 0) {
|
||||||
|
let currentNode = openList[0];
|
||||||
|
let currentIndex = 0;
|
||||||
|
openList.forEach((item, index) => {
|
||||||
|
if (item.f < currentNode.f) {
|
||||||
|
currentNode = item;
|
||||||
|
currentIndex = index;
|
||||||
}
|
}
|
||||||
if (isDeadEnd(pos)) {
|
});
|
||||||
fillDeadEnd(pos);
|
|
||||||
|
openList.splice(currentIndex, 1);
|
||||||
|
closedList.push(currentNode);
|
||||||
|
|
||||||
|
if (currentNode.pos.x === endNode.pos.x && currentNode.pos.y === endNode.pos.y) {
|
||||||
|
let current: Nd|null = currentNode;
|
||||||
|
while (current) {
|
||||||
|
path.push(current.pos);
|
||||||
|
current = current.parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const children: Array<Nd> = [];
|
||||||
|
|
||||||
|
[Dir.Up, Dir.Right, Dir.Down, Dir.Left].forEach((dir) => {
|
||||||
|
const nodePos = nextCoord(currentNode.pos, dir);
|
||||||
|
|
||||||
|
if (get(nodePos) === '#') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newNode: Nd = {
|
||||||
|
parent: currentNode,
|
||||||
|
pos: nodePos,
|
||||||
|
g: 0,
|
||||||
|
h: 0,
|
||||||
|
f: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (closedList.find((closedChild) => closedChild.pos.x === newNode.pos.x && closedChild.pos.y === newNode.pos.y)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
newNode.g = currentNode.g + 1;
|
||||||
|
newNode.h = ((newNode.pos.x - endNode.pos.x) ** 2) + ((newNode.pos.y - endNode.pos.y) ** 2);
|
||||||
|
newNode.f = newNode.g + newNode.h;
|
||||||
|
|
||||||
|
if (openList.find((openNode) => newNode.pos.x === openNode.pos.x && newNode.pos.y === openNode.pos.y && newNode.g > openNode.g)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
openList.push(newNode);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
printMap();
|
printMap();
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
|
||||||
let fastest: number|null = null;
|
let fastest: number|null = null;
|
||||||
|
|
||||||
@@ -175,3 +265,6 @@ const fastestScore = Math.min(...scores.map((p) => p.length));
|
|||||||
const fastestPath = scores.find((p) => p.length === fastestScore);
|
const fastestPath = scores.find((p) => p.length === fastestScore);
|
||||||
printMap(fastestPath);
|
printMap(fastestPath);
|
||||||
console.log(fastestScore);
|
console.log(fastestScore);
|
||||||
|
=======
|
||||||
|
console.log(path, path.length)
|
||||||
|
>>>>>>> Stashed changes
|
||||||
|
|||||||
Reference in New Issue
Block a user