function findRoute(routeMap, from, to, secureLevel) {
let route;
while (!Array.isArray(route = tryFindRoute(routeMap, from, to, secureLevel))) {
let invalidRoute, invalidRouteIndex;
invalidRoute = routeMap[route][6]
.sort((a, b) => a - b)
.reverse()
.find(e => e > from && e > route);
invalidRouteIndex = routeMap[route][6].indexOf(invalidRoute);
routeMap[route][6].splice(invalidRouteIndex, 1);
}
return route
}
function tryFindRoute(routeMap, from, to, secureLevel) {
let route = [from], routePoint;
for (let i = 0, len = routeMap.length; i < len; i++) {
let el = routeMap[i];
if (route[route.length - 1] > el[0]){
continue
}
routePoint = el[6]
.sort((a, b) => a - b)
.reverse()
.find(e => e > from && e > i);
if (el[0] !== to) {
if (!routePoint || el[5] < secureLevel) {
route = route[Math.max(0, route.length - 2)];
break
}
route.push(routePoint)
}
}
return route;
}
let x, y, z;
let routeMap = [
[0, "name", x, y, z, 10, [1, 2]],
[1, "name", x, y, z, 10, [0, 3]],
[2, "name", x, y, z, 5, [0, 3, 4]],
[3, "name", x, y, z, 10, [2, 1, 4]],
[4, "name", x, y, z, 10, [3, 2, 5]],
[5, "name", x, y, z, 10, [4]]
];
let shortRoute = findRoute(routeMap, 0, 5, 0);
let inSecureshortRoute = findRoute(routeMap, 0, 5, 10);
console.log('самый короткий из безопасных путей', inSecureshortRoute);
console.log('самый короткий путь', shortRoute);