Tromino问题

3 篇文章 0 订阅
3 篇文章 0 订阅
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;
	}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值