BFS ????? ???? ???? ???? ?? ??? ?????.
P粉633075725
2023-09-03 11:42:48
<p>? ????? ??? ??? ??? ???? ?? ?? ?? ????? ???? PHX? BKK ??? ?? ??? ???? ????. <strong>??? ??? ???? ? ??? ????. </strong></p>
<p>???? ??(?? ??)? ??? ????. PHX -> LAX -> BKK</p>
<pre class="brush:php;toolbar:false;">const Airports = 'PHX BKK OKC JFK LAX MEX EZE HEL LOS LAP LIM'.split(' ');
const ?? = [
['PHX', 'LAX'],
['PHX', 'JFK'],
['JFK', 'OKC'],
['JFK', '?'],
['JFK', 'LOS'],
['MEX', 'LAX'],
['MEX', 'BKK'],
['??', '?'],
['MEX', 'EZE'],
['?', 'BKK'],
];
//???
const adjacencyList = new Map();
//?? ??
?? addNode(??) {
adjacencyList.set(??, []);
}
// ??? ???? ?? ???? ??
function addEdge(???, ???) {
adjacencyList.get(origin).push(destination);
adjacencyList.get(destination).push(origin);
}
// ??? ??
Airports.forEach(addNode);
// ? ??? ???? ?? addEdge ??? ??????.
Routes.forEach(route => addEdge(...route));</pre>
<p>??? ???(???)?? ?? ??? ???? ?? ???? ??? ????</p>
<pre class="brush:php;toolbar:false;">function bfs(start) {
const ?? = new Set();
Visited.add(start); // ?? ??? ?? ??? ?????.
const ??? = [??];
while (queue.length > 0) {
const Airport = queue.shift(); // ??? ??
const ??? = adjacencyList.get(airport);
for (???? ?? ???) {
if (?? === 'BKK') {
console.log(`BFS? ??? ?????!`)
//console.log(??);
}
if (!visited.has(???)) {
??.??(???);
queue.push(??);
}
}
}
}
bfs('PHX')
InSync? ?? ??? ?? ??? ??? ? ?????
bfs() ???? oldpath? ? ??(?? ??)? ??? ??? ???? ? ???? ?? ??? ??? ???? ? ?????
???
???
? ??? ??? ?? ?????. shortestPath? ?? ??? ??? ?? ?? ??? ?? ??? ??(???? ??) ?? ?? ??? ?? ??? ? ??? ?????.
???
?? +0
P粉633075725
??? ??? ??? ???? ?? ? ??? ???? ??? ?? ??? ?????. ??? ???? ??? ??? ? ????.
- ?? ?? ??? ?????
- ? ???? ?? ?? ??(?? ??)? ?????.
- ??? ???? ??? ??? ?????
?? ???? ?? ??? ???? ?? ??? ?? ??? ?? ?? ????? ???? ?? ????.
?? +0
P粉633075725