动态规划之切钢条

 

    //填写备忘录表。查询。
    //从低到高递归填写备忘录。
    //最初数据,经过几次抉择,产生更多数据。
    public static class dynamicProcess
    {
        public static void test()
        {
            int[] specification={0,1,5,8,9,10,17,17,20,24,30};
        
            
            //fill noteTable.
            Integer[] NoteTable=new Integer[6];
            NoteTable[0]=0;
            fillTable(NoteTable, specification);
            
            int length=5;
            LSLog.printLine(length+" best:"+NoteTable[length], 1);
        }
        
        
        private static void fillTable(Integer[] table,int[] specifition)
        {
            //get each question's best value from 1 to target.
            for(int i=1;i<=table.length-1;i++)
            {
                //get all group. compare each group's value,and get bestValue.
                MyLinkedList<MyLinkedList<Integer>> ret=resultList(i,i-1);
                int bestValue=bestValue(ret,specifition,i,table);
                LSLog.printLine("---------------------------", 1);
                table[i]=bestValue;
            }
        }
        
        
        //size:原始数据 .level:进行第几个抉择。
        //对一个多抉择点问题,进行递归抉择。分解:n-1 的结果删除最后一个,再插入2中可能。最小解:level=0,无抉择,创建初值。
        private static MyLinkedList<MyLinkedList<Integer>> resultList(Integer size,Integer level)
        {
            assert(size!=null&& size>0):"data is illvalible.";
            if(level==0)
            {
                MyLinkedList<Integer> temp=new MyLinkedList<Integer>();
                temp.add(size);
                MyLinkedList<MyLinkedList<Integer>> bbLinkedList=new MyLinkedList<MyLinkedList<Integer>>();
                bbLinkedList.add(temp);
                return bbLinkedList;
                
            }
            else 
            {
                MyLinkedList<MyLinkedList<Integer>> tempret=resultList(size, level-1);
                MyLinkedList<MyLinkedList<Integer>> tempnew=new MyLinkedList<MyLinkedList<Integer>>();
                for(int i=0;i<tempret.size();i++)
                {
                    MyLinkedList<Integer> theOneCopy=tempret.get(i).clone();
                    MyLinkedNode<Integer> theNode=theOneCopy.getNode(theOneCopy.size()-1);
                    Integer theValueInteger=theNode.mdata;
                    
                    int cutValue= level-(size-theOneCopy.mLast.mPre.mdata);
                    
                    theOneCopy.remove(theOneCopy.getNode(theOneCopy.size()-1));
                    theOneCopy.add(cutValue);
                    theOneCopy.add(theValueInteger-cutValue);
                    
                    tempnew.add(theOneCopy);
                }
                
                for(int i=0;i<tempnew.size();i++)
                {
                    tempret.add(tempnew.get(i));
                }
                
                return tempret;
            }
        }
        private static int bestValue(MyLinkedList<MyLinkedList<Integer>> ret,int[] specifition,int level,Integer[] table)
        {
            
            int bestValue=-1;
            
            for(int i=0;i<ret.size();i++)
            {
                int tempValue=0;
                MyLinkedList<Integer> theAnswer=ret.get(i);
                for(int h=0;h<theAnswer.size();h++)
                {
                    LSLog.printLine(theAnswer.get(h)+":"+specifition[theAnswer.get(h)], 1);
                    int chooseValue=specifition[theAnswer.get(h)];
                    if(table[theAnswer.get(h)]!=null && table[theAnswer.get(h)]!=0)
                    {
                        chooseValue=table[theAnswer.get(h)];
                    }
                    tempValue+=chooseValue;
                }
                bestValue=bestValue>tempValue?bestValue:tempValue;
                LSLog.printLine("******"+tempValue +".best."+bestValue, 1);
            }
            
            return bestValue;
        }
        
    }
    

    

 

posted @ 2019-03-26 10:23  琴鸟  阅读(379)  评论(0编辑  收藏  举报