背包

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 @ 2017-01-07 10:30  王若伊_恩赐解脱  阅读(259)  评论(0编辑  收藏  举报