找到最大和,使得 n 叉树中没有两个元素相邻

问题描述

我希望设计一种动态规划方法,根据给定的标准选择狮子以最大化总狩猎能力

标准:狮子不能和直系父母一起去打猎

我设计了一种算法,但该算法无法正常工作。该算法有什么问题?

树示例(左子右兄弟表示):

enter image description here

节点:

    Lion data;
    Node next,child;
            
    public Node(Lion data)
    {
        this.data = data;
        next = child = null;
    }

最大化方法

private int findParentHuntingAbility(Node root) {
        return root.data.getHuntingAbility();
        
    }

private int findKidsHuntingAbility(Node root) {
        int sumOfKidsHuntingAbility = 0;
        if(root.child != null) {
            root = root.child;
            while(root != null) {
                sumOfKidsHuntingAbility  += root.data.getHuntingAbility();
                root = root.next;
            }
            
        } 
        
        return sumOfKidsHuntingAbility;
        
    }

最大化狩猎能力的算法:

 //Traverses tree in depth first order
    public int findMaxHuntingAbility(Node root)
    {
        int maxHuntingAbility = 0;
        
        if(root == null)
            return 0;
        
        while(root != null)
        {
            
            if(root.child != null)
                findMaxHuntingAbility(root.child);
            
            maxHuntingAbility += Math.max(findKidsHuntingAbility(root),findParentHuntingAbility(root));
            root = root.next;
           
        }
        
        return maxHuntingAbility;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)