function countPatternsFrom2(firstPoint, length) {
if (length > 9 || length == 0) return 0;
if (length == 1) return 1;
if (length == 2) return O[firstPoint].length;
let sum = 0, stack = [firstPoint], actived = [], id = 0, d = true;
while (stack.length) {
if (id > -1) stack.push(O[stack.slice(-1)[0]][id]);
let v0 = stack.slice(-1)[0], w = stack.slice(-2)[0], x = stack.length;
if (x + 1 == length) {
O[v0].forEach(v => { if (!stack.includes(v)) sum++ });
for (let key in Active) {
if (stack.includes(key) && v0 in Active[key] && !stack.includes(Active[key][v0])) {
sum++
}
};
d = false
}
if (d) {
id = O[v0].findIndex(v => !stack.includes(v));
if (id == -1) {
let L = '';
for (let key in Active) {
if (stack.includes(key) && v0 in Active[key] && !stack.includes(Active[key][v0])) {
L += Active[key][v0]
}
}
if (L.length) {
stack[x] = L[0];
actived[x] = L.slice(1)
}
}
continue
};
if (actived[x - 1] == `${actived[x - 1]}`) {
if (actived[x - 1].length) {
stack[x - 1] = actived[x - 1][0];
actived[x - 1] = actived[x - 1].slice(1);
d = true
} else {
stack.length--;
actived.length = stack.length
}
continue
}
id = O[w].findIndex((v, i) => i > O[w].indexOf(v0) && !stack.includes(v));
stack.pop();
if (id == -1) {
let L = '';
for (let key in Active) {
if (stack.includes(key) && w in Active[key] && !stack.includes(Active[key][w])) {
L += Active[key][w]
}
}
if (L.length) {
stack[x - 1] = L[0];
actived[x - 1] = L.slice(1);
d = true
}
} else d = true
}
return sum
}
console.log(countPatternsFrom2('E', 9));
function countPatternsFrom2(firstPoint, length) {
if (length > 9 || length < 1) return 0;
let sum = 0, stack = [firstPoint], que = [''], down = true;
while (stack.length) {
let x = stack.length, v0 = stack[x - 1];
if (x == length) {
sum += que[x - 1].length + 1;
down = false;
stack.pop();
que.pop();
x--
}
if (down) {
let L = '';
O[v0].forEach(v => { if (!stack.includes(v)) L += v });
for (let key in Active) {
if (stack.includes(key) && v0 in Active[key] && !stack.includes(Active[key][v0])) {
L += Active[key][v0]
}
}
stack.push(L[0]);
que.push(L.slice(1))
} else {
if (que[x - 1]) {
stack[x - 1] = que[x - 1][0];
que[x - 1] = que[x - 1].slice(1);
down = true
} else {
stack.pop();
que.pop()
}
}
}
return sum
}
console.log(countPatternsFrom2('E', 9))
function countPatternsFrom2(firstPoint, length) {
if (length == 1) return 1;
if (length > 9) return 0;
let sum = 0, visited = [[firstPoint]], next = [];
for (let counter = length; counter > 0; counter--) {
visited.forEach(v => {
let x = v.length + 1;
O[v[0]].forEach(w => {
if (!v.includes(w)) x == length ? sum++ : next.push([w].concat(v))
});
for (key in Active) {
if (v.includes(key) && v[0] in Active[key]) {
if (!v.includes(Active[key][v[0]])) x == length ? sum++ :
next.push([Active[key][v[0]]].concat(v))
}
}
});
visited = next.slice();
next = [];
}
return sum
}
console.log(countPatternsFrom2('E', 9))
if (x == length) {
id = O[w].findIndex((v, i) => i > O[w].indexOf(v0) && !stack.includes(v));
stack.pop();
sum++;
if (id == -1) {
for (key in Active) {
if (stack.includes(key) && w in Active[key] && !stack.includes(Active[key][w])) {
sum++
}
}
}
continue
}
if (x + 1 == length) {
O[v0].forEach(v => { if (!stack.includes(v)) sum++ });
for (key in Active) {
if (stack.includes(key) && v0 in Active[key] && !stack.includes(Active[key][v0])) {
sum++
}
}
id = -1
}
console.log('5 /(2- 3)* 1.333333 + -11 '.match(/\+|\(|\)|\-|\*|\/|[\d.]+/g))
.replace(/\s/g,'')
, а отделить десятичное число не пойму как:let st='5 /(2- 3)* 1.333333 + -11 ';
console.log(st.replace(/\s/g,'').match(/\d+|\+|\(|\)|\-|\*|\/|[\d.]/g))
Количество сгенерированных вариантов соединений всех узлов равно 2^(k - 1) * k! Для 3, 4, 5, 6 узлов это будет 4, 24, 192, 1920 вариантов соответственно. Количество запусков поиска пути будет равно сумме поисков на каждом уровне генерации сегментов, то есть 8, 54, 440, 4410 для 3, 4, 5, 6 узлов соответственно, найдено эмпирическим методом, общая формула похожа на сумму арифметико-геометрической прогрессии. Ниже приведен код на JS для подсчета вариантов соединений и поисков пути.