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问题
最新推荐文章于 2021-11-02 21:22:12 发布