背包

复制代码
import java.util.Arrays;

public class Knapsack
{
    public static void main(String args[])
    {
        int[] array = new int[]
        { 2, 98, 12, 3, 4, 6, 7, 8, 9, 33, 44, 56, 78, 99,50,50 };
        int capacity = 279;
        int[] resultArray = calc(capacity, array);
        int sum = sum(resultArray, 0);
        System.out.println(Arrays.toString(resultArray));
        System.out.println(sum);
    }
    
    private static int sum(int[] array, int currentWeight)
    {
        int result = 0;
        for (int i : array)
        {
            result += i;
        }
        return result + currentWeight;
    }
    
    private static int[] calc(int capacity, int[] array)
    {
        if (array.length == 0)
        {
            return new int[0];
        }
        if (array.length == 1)
        {
            if (capacity > array[0])
            {
                return array;
            }
            return new int[0];
        }
        if (capacity < array[0])
        {
            return calc(capacity, Arrays.copyOfRange(array, 1, array.length));
        }
        int[] subResultWithCurrentNode = calc(capacity - array[0], Arrays.copyOfRange(array, 1, array.length));
        int[] subResultWithOutContainCurrentNode = calc(capacity, Arrays.copyOfRange(array, 1, array.length));
        int sumWithCurrentNode = sum(subResultWithCurrentNode, array[0]);
        int sumWithOutCurrentNode = sum(subResultWithOutContainCurrentNode, 0);
        if (sumWithCurrentNode < sumWithOutCurrentNode)
        {
            return subResultWithOutContainCurrentNode;
        }
        int[] result = new int[subResultWithCurrentNode.length + 1];
        result[0] = array[0];
        System.arraycopy(subResultWithCurrentNode, 0, result, 1, subResultWithCurrentNode.length);
        return result;
    }
    
}
复制代码

 

import java.util.ArrayList; import java.util.List;

public class AAA {  int height = 20;  int width = 12;  int[][] map = new int[12][20];  int curBlockValue = -1;  int emptyBlockValue=-1;    public float getScore(int[][] mapBoard, int[][] curBlock, int colIndex)  {   copyBoard(mapBoard);   setBlockToBoard(curBlock, colIndex);   dropDownCurBlock(curBlock);   List<Integer>list=getKillRowIndex();   return calScore(list);  }    private float calScore(List<Integer> list)  {   // TODO Auto-generated method stub   return 0;  }

 private void dropDownCurBlock(curBlock)  {   for (int rowIndex = height; rowIndex >= 0; rowIndexs++)   {    List<Integer> row = getRow(rowIndex);     List<Integer> colList=  getCurRowContainCurBlockColIndex(row);    if (colList.size()==0)    {     continue;    }    if(checkCurCurBlockHasDropSpace(colList,rowIndex))    {     moveDownCurBlock(rowIndex,curBlock);    }     return;   }     }    private void moveDownCurBlock(rowIndex,curBlock)  {   for(int i=0;i<width;i++)   {    for()   }  }

 private List<Integer> getRow(int rowIndex)  {   List<Integer> rowList = new ArrayList<Integer>();   for (int i = 0; i < width; i++)   {    rowList.add(map[i][rowIndex]);   }   return rowList;  }    private boolean checkCurCurBlockHasDropSpace(List<Integer> colList, int rowIndex)  {   if (rowIndex==20)   {    return false;   }   for (Integer colIndex : colList)   {    if (map[colIndex][rowIndex+1]!=emptyBlockValue)    {     return false;    }   }   return true;  }    private List<Integer> getCurRowContainCurBlockColIndex(List<Integer> row)  {   List<Integer> colIndexList = new ArrayList<Integer>();      for (int i = 0; i < width; i++)   {    int j = row.get(i);    if (j == curBlockValue)    {     colIndexList.add(i);    }   }   return colIndexList;  }    private void setBlockToBoard(int[][] curBlock, int colIndex)  {   for (int i = 0; i < curBlock.length; i++)   {    int[] js = curBlock[i];    for (int j = 0; j < js.length; j++)    {     map[i + colIndex][j] = curBlockValue;    }   }  }    private void copyBoard(int[][] mapBoard)  {   for (int i = 0; i < mapBoard.length; i++)   {    for (int j = 0; j < mapBoard[i].length; j++)    {     map[i][j] = mapBoard[i][j];    }   }  } }

 

posted @   王若伊_恩赐解脱  阅读(262)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)

点击右上角即可分享
微信分享提示