first of all, sorry about my bad english and thx for the time you spend reading this.
this is a functios from a problem of 2D packing( a 2d boards with 2d tiles, like the knapsack, returning boolean if the tiles can fit iniside the board)
i tried to make a function that getting a board in different situations, and a tile, and return an array wich contain all the coardinates that avaliable for the corrent tile.
for example, a board as big as 2X2, and a tile of 1x1, the function will return an array like:
00
01
10
11
the problem is with the fact that i dont know how many coards will be, before initalizing the array.
so i managed to make another function that removing the unwanted "000000" in the end.
(sound aufull, i know, but i cant use Objects because we didnt leaned it yet)
the problem starts when my function sometimes leaves another 0,0 in the end.
any alternative or solutions??
PHP Code:
public static int[][] allAvaliablePlaces(int[][] board, int length, int width){
int[][] coards = new int[board.length*board[0].length][2];
int index = -1; // will index the new array, and chek if there are coardinates at all
for(int i = 0 ; i < board.length ; i++) // i run over rows
for(int j = 0 ; j < board[0].length ; j++) // j run over columns
if(placeValid(board, i, j, width, length)) { // chek if the place is valid
index++;
coards[index][0] = i; // the row component
coards[index][1] = j; // the col comp
}
int[][] finalcoards = cuttingZeroes(coards);
if(index == -1)
finalcoards = null; // returns null if index is same as the begining
return finalcoards;
}
public static int[][] cuttingZeroes(int[][] coards) {
for( int i = 0 ; i < coards.length-1 ; i++)
if(coards[i][0] == coards[i][1] && coards[i+1][0] == coards[i+1][1] && coards[i][0] == 0)
{
int[][] ans = new int[i][2];
for(int j = 0; j < i; j++){
ans[j][0] = coards[j][0];
ans[j][1] = coards[j][1];
}
return ans;
}
return coards;
}