static int HomeCols = 4; static ListlistRows = new ArrayList (); private static void setNodeRowCol(BaseNode node, int row, int col){ node.setRowNum(row); node.setColNum(col); for(int cellRow = row; cellRow < row + node.getRows(); cellRow ++){ for(int cellCol = col; cellCol < col + node.getCols(); cellCol ++){ listRows.get(cellRow)[cellCol] = 1; } } } /** * * @param node * @param fromRow * @param fromCol * @return */ private static boolean hasEmptyPosition(BaseNode node, int fromRow, int fromCol){ if(fromCol + node.getCols() > HomeCols || fromRow + node.getRows() > listRows.size()){ return false; } for(int i = fromRow; i < fromRow + node.getRows() ; i ++){ for(int j = fromCol; j < fromCol + node.getCols() ; j ++){ if(listRows.get(i)[j] == 1){ return false; } } } return true; } private static int[] getNodePlace(BaseNode node, int row, int col){ //如果没有足够位置,则先生成位置。 if(listRows.size() < row + node.getRows()){ listRows.add(new int[HomeCols]); } boolean hasPos = false; int[] xx; System.err.println("row:"+node.getRowNum() + " col"+node.getColNum() +" "+ row +" >>>"+ col); //从当前行循环到最后一个起始行(listRows.size() - node.getRows()) for(int nodeRow = row ; nodeRow <= listRows.size() - node.getRows(); nodeRow ++){ //从当前列循环到最后一个起始列(HomeCols - node.getCols()) for(int nodeCol = col ; nodeCol <= HomeCols - node.getCols(); nodeCol ++){ //如果当前行列被占用,则从当前行下一列开始重新搜索 //如果是最后一列,则从下一行第一列开始重新搜索 if(listRows.get(nodeRow)[nodeCol] == 1){ if(col + 1 == HomeCols){ xx = getNodePlace(node, row + 1, 0); }else{ xx = getNodePlace(node, row, col + 1); } if(xx[0] >= 0){ return xx; } }else{ //如果当前行列为空闲,则搜索该行列开始的node所覆盖的行列是否全部为空闲 hasPos = hasEmptyPosition(node, nodeRow, nodeCol); //如果以当前行列为起始位置,存在可用的空闲区域,则退出,搜索成功 if(hasPos){ break; }else{ //如果以当前行列为起始位置,不存在可用的空闲区域 //如果不是最后一列,则从当前行下一列开始重新搜索 //如果是最后一列,则从下一行第一列开始重新搜索 if(col + 1 == HomeCols){ xx = getNodePlace(node, row + 1, 0); }else{ xx = getNodePlace(node, row, col + 1); } if(xx[0] >= 0){ return xx; } } } } if(hasPos){ break; }else{ if(col + 1 == HomeCols){ xx = getNodePlace(node, row + 1, 0); }else{ xx = getNodePlace(node, row, col + 1); } if(xx[0] >= 0){ return xx; } } } node.setRowNum(row); node.setColNum(col); setNodeRowCol(node, row, col); return new int[]{row, col}; }
测试代码:
BaseNode node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); int[] xxx = getNodePlace(node, 0 , 0); System.err.println("should 0,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 0,2 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(4); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 1,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 2,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 2,2 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(4); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 3,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 4,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(2); node.setCols(4); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 5,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(4); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 7,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 4,2 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 8,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 8,2 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 9,0 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(2); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 9,2 ------------------------- "+node.getRowNum() + "," +node.getColNum()); node = new BaseNode(); node.setRows(1); node.setCols(2); printlistRows(); xxx = getNodePlace(node, 0 , 0); System.err.println("should 10, 0 ------------------------- "+node.getRowNum() + "," +node.getColNum());