import java.util.Random;
public class Tromino {
private static int N = 3;
private int[][] chessboard;
private Point fisrtBlackALattice = new Point();
public Tromino() {
int nPower = this.get2NPower(this.N);
chessboard = new int[nPower][nPower];
this.blackedOutFirstLattice();
this.printChessboard();
}
private void blackedOutFirstLattice() {
Random random = new Random();
int x = random.nextInt(chessboard[0].length);
int y = random.nextInt(chessboard[0].length);
fisrtBlackALattice.x = x;
fisrtBlackALattice.y = y;
blackedOutALattice(fisrtBlackALattice);
}
private void blackedOutALattice(Point aLattice) {
chessboard[aLattice.x][aLattice.y] = 1;
}
public void blackedOutChessboard() {
blackedOutLattices(this.N, new Point(0, 0));
}
private void blackedOutLattices(int n, Point north_west) {
if (n == 1) {// 2*2的棋盤
// System.out.println("2");
blackedOutThree(north_west);
return;
} else {
int length = this.get2NPower(n);
int halflength = length / 2;
System.out.println(length);
Point[] northwest = new Point[4];
northwest[0] = new Point(north_west.x, north_west.y);
northwest[1] = new Point(north_west.x + halflength, north_west.y);
northwest[2] = new Point(north_west.x, north_west.y + halflength);
northwest[3] = new Point(north_west.x + halflength, north_west.y
+ halflength);
int areaNum = -1;
while (!this.isContainBlackLattice(northwest[++areaNum], halflength)) {
//areaNum++;
}
//System.out.println(areaNum);
// 把沒有黑方塊的其他三個區域的靠近中心的方格塗黑
for (int i = 0; i < 4; i++) {
if (i == areaNum) {
continue;
} else {
if (i == 0) {// 塗東南格子,0區域的東南格子挨着3區域的西北格子的x、y座標各減1
this.blackedOutALattice(new Point(northwest[3].x - 1,
northwest[3].y - 1));
} else if (i == 1) {// 塗1區域的西南格子
this.blackedOutALattice(new Point(northwest[3].x,
northwest[3].y - 1));
} else if (i == 2) {// 塗2區域的東北格子
this.blackedOutALattice(new Point(northwest[3].x - 1,
northwest[3].y));
} else if (i == 3) {// 塗3區域的西北格子
this.blackedOutALattice(new Point(northwest[3].x,
northwest[3].y));
}
}
}
for (int i = 0; i < 4; i++) {
blackedOutLattices(n - 1, northwest[i]);
}
}
}
private void blackedOutThree(Point northwest) {
this.blackedOutALattice(new Point(northwest.x, northwest.y));
this.blackedOutALattice(new Point(northwest.x + 1, northwest.y));
this.blackedOutALattice(new Point(northwest.x, northwest.y + 1));
this.blackedOutALattice(new Point(northwest.x + 1, northwest.y + 1));
}
private boolean isContainBlackLattice(Point northwest, int length) {
Point southeast = new Point();
southeast.x = northwest.x + length - 1;
southeast.y = northwest.y + length - 1;
for (int x = northwest.x; x <= southeast.x; x++) {
for (int y = northwest.y; y <= southeast.y; y++) {
if (this.chessboard[x][y] == 1) {
System.out.println(x + " ," + y);
return true;
}
}
}
return false;
}
private void printChessboard() {
for (int x = 0; x < chessboard[0].length; x++) {
for (int y = 0; y < chessboard[0].length; y++) {
System.out.print(chessboard[x][y] + " ");
}
System.out.println();
}
}
private int get2NPower(int n) {
int res = 1;
for (int i = 0; i < n; i++) {
res *= 2;
}
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Tromino tro = new Tromino();
tro.blackedOutChessboard();
tro.printChessboard();
}
}
class Point {
int x;
int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Tromino問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.