java - How should I fit a shape into a space? -
suppose have following shape (string type);
..ee e..e and following space:
||.. .|.. my method shapefitsat() determines if given shape may placed @ indicated row/col position. row,col indicates upper left corner [block (0,0)] of shape placed. shape not fit if 1 of filled blocks conflict existing filled block in space or out of bounds in space. "|" means block/obstacle while "." means empty space. if place above shape space, this:
||ee e|.e please can smb how approach fitting shape space? in advance!
public class createspace { private int height; private int width; private char[][] space = new char[height][width]; private shape originalshape; public createspace(int height, int width, char[][] space, shape shape) { this.height = height; this.width = width; this.space = space; this.originalshape = shape; } public boolean shapefitsat(int row, int col, shape shape) { if(row < 0 || row >= height || col < 0 || col >= width) throw new fititexception("oops! out of bounds in createspace class! go , check!"); else if(space[row][col] == '|' || space[row][col] == this.originalshape.getdisplaychar()) throw new fititexception("the space position filled out wall or character. go check createspace class!"); else { } }
a solution "linearize" patterns 2 bitsets (one shape, 1 obstacles).
put 1's symbols must preserved in shape bitset, , 0's averywhere else ; put 0's obstacles are, , 1's elsewhere in "obstacle" bitset.
ex:
..ee --> ..eee..e --> 00111001 e..e ||.. --> ||...|.. --> 00111011 .|.. then , 2 bitsets, , see if result same shape bitset. if if, shapes fit ; if not, @ least 1 obstable in way.
shape : 00111001 obstacles : 00111011 , : 00111001 == shape bitset => ok ! quick , efficient, , can re-use obstacle bitset test multiple shape patterns.
Comments
Post a Comment