For a university project, we have to develop an algorithm that finds out how many oslutions there are to a 6x5 pentomino grid.
The breadth-first algorithm that we developed looks like this.
_________________________
import java.util.ArrayList;
public class SolutionCalculator
{
//This object stores all the pentominos
private BlockStorer blocks = new BlockStorer();
//this is the solutioncounter
private int solutions=0;
//this the initial grid list (only 1 empty grid.
private ArrayList<Grid> grids = new ArrayList<Grid>();
//a variable to store a Block(=pentomino)
private Block block;
//stores a Grid
private Grid grid2;
public SolutionCalculator()
{
//This one starts the class by adding the empty grid to the grids list
grids.add(new Grid());
}
public int calc()
{
//This one starts the algorithm, with the earlier declared list of grids, it starts a recursive algorithm with this function with an argument;.
System.out.println("starting now...");
return calc(grids);
}
public int calc(ArrayList<Grid> list)
{
System.out.println("_________NEW GENERATION_________");
//This is the empty list that will form the next list for this function (the next generation in the search tree.)
ArrayList<Grid> nextgen = new ArrayList<Grid>();
//loops through the grids in this generation, it will try placing every pentomino on the found free spot
for(Grid grid : list)
{
System.out.println("checking the following grid");
grid.printGrid();
//loops through all the pentominos
for(int blocknr=0; blocknr<12; blocknr++)
{
//Gets the first pentomino (the arguments are blocknumber and orientation)
block=blocks.getBlock(0,0);
//loops through all the rotations of a block
for(int rotation=0; block!=null && rotation<5; block=blocks.getBlock(blocknr,rotation))
{
rotation++;
//get the position to place the block
int[] spot = grid.findSpot();
System.out.pGrintln("We copy the grid and add the block to the grid at position("+spot[0]+","+spot[1]+")");
create a new Grid object, that will have the same blocks on it as the current grid in this generation
grid2 = new Grid(grid.getGrid());
System.out.println("Before adding, the grid looks like this");
grid.printGrid();
System.out.println();
grid2.printGrid();
//place down the block on the newly created grid
boolean success=grid2.addBlock(block,spot[0],spot[1]);
//if that did not cause any errors
if(success)
{
//if the grid is full, ad 1 to the number of solutions
if(grid2.isFull())
solutions++;
else
{
//else add this grid to the next generation
nextgen.add(grid2);
System.out.println("the placement succeeded, so we added this grid to the next generation.");
}
}
}
}
}
//repeat this function if the next generation is not empty.
if(!nextgen.isEmpty())
calc(nextgen);
//return the number of solutions
return solutions;
}
//a main method for testing
public static void main(String[] args)
{
SolutionCalculator solver = new SolutionCalculator();
int solutions = solver.calc();
System.out.println(solutions);
}
}
________________________
Now, the problem is, that instead of every time a next 'node' in a generation is tested by placing a block, the program uses the grid with the previously placed block in it. It should only use the blocks that are in the grid from the grids list.
If you need the other classes to know what goes wrong, tell me, in that case, I'll upload the entire project to a filehost ande post the link here.
Edited by: frederik_c on 18-okt-2010 6:49