- var makeArray = function(obj) {
- return Array.prototype.slice.call(obj);
- }
- function sum(arr, tree) {
- var s = 0,
- len = arr.length,
- node;
- while(len--){
- node = arr[len];
- var a = tree[node[0]][node[1]];
- s += Number(a);
- }
- return s;
- }
- function getTree() {
- var tree = [],
- len,
- box1 = makeArray(document.getElementById('box').childNodes),
- len1 = box1.length,
- box2;
- while (box1.length) {
- box2 = makeArray((box1.shift()).childNodes);
- len = tree.length;
- tree[len] = [];
- while (box2.length) {
- tree[len].push(box2.shift().innerText);
- }
- }
- return tree;
- }
- function deepSearch(tree) {
- var tLen = tree.length,
- routeList = [],
- max = 0,
- maxRoute;
- (function search(node, route) {
- var currentRoute = route.concat([]);
- currentRoute.push(node);
- var nodeX = node[0],
- nodeY = node[1];
- if (nodeX +1 >= tLen) {
- currentRoute.sum = sum(currentRoute, tree);
- if (max - currentRoute.sum < 0) {
- max = currentRoute.sum;
- maxRoute = currentRoute;
- }
- routeList.push(currentRoute);
- return;
- }else{
- search([nodeX + 1, nodeY], currentRoute);
- search([nodeX + 1, nodeY + 1], currentRoute);
- }
- })([0,0],[]);
- return maxRoute;
- }
- var tree = getTree();
- var maxRoute = deepSearch(tree);
- console.log('maxRoute:',maxRoute);
- //该片段来自于http://www.codesnippet.cn/detail/240220148759.html
来源: http://www.codesnippet.cn/detail/240220148759.html