java 棋盘覆盖算法

import java.util.Scanner;


/** 棋盘覆盖 */
public class Arithmetic {


/** 被覆盖后显示的数字,会根据覆盖的顺序有所递增 */
private int counter = 0;
/** 当前棋盘是否已经存在 */
private boolean hasMap = false;
/** 棋盘数组 */
private int[][] map = null;


/**
* 棋盘覆盖算法

* @param northWestX
*            :棋盘左上角的X坐标
* @param northWestY
*            :棋盘左上角的Y坐标
* @param targetX
*            :特殊位置的X坐标
* @param targetY
*            :特殊位置的Y坐标
* @param size
*            :棋盘大小
*/
public void chessBoard(int northWestX, int northWestY, int targetX,
int targetY, int size) {


if (!hasMap) {
buildMap(size);
hasMap = true;
}
/**
* 如果棋盘大小为1就退出
*/
if (size == 1) {
return;
}


/**
* 将棋盘大小除以2,将问题规模缩小
*/
size /= 2;


/** 用于标记当前覆盖位置 */
int sign = ++counter;


/**
* 1.如果目标在当前棋盘的左上角,则递归求解
*/
if (targetX < northWestX + size && targetY < northWestY + size) {
chessBoard(northWestX, northWestY, targetX, targetY, size);
} else {
/**
* 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错
*/
if (map[northWestY + size - 1][northWestX + size - 1] != 0) {
System.out
.println("position (" + (northWestX + size - 1) + ", "
+ (northWestY + size - 1)
+ ") has been filled!");
}


/**
* 重新指定目标代码为右下角方格
*/
map[northWestY + size - 1][northWestX + size - 1] = sign;


/** 递归求解当目标代码为右下角方格时的情况 */
chessBoard(northWestX, northWestY, northWestX + size - 1,
northWestY + size - 1, size);
}


/**
* 2.如果目标在当前棋盘的右上角,则递归求解
*/
if (targetX >= northWestX + size && targetY < northWestY + size) {
chessBoard(northWestX + size, northWestY, targetX, targetY, size);
} else {
/**
* 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错
*/
if (map[northWestY + size - 1][northWestX + size] != 0) {
System.out.println("position (" + (northWestX + size) + ", "
+ (northWestY + size - 1) + ") has been filled!");
}


/**
* 重新指定目标代码为坐下角方格
*/
map[northWestY + size - 1][northWestX + size] = sign;


/** 递归求解当目标代码为左下角方格时的情况 */


chessBoard(northWestX + size, northWestY, northWestX + size,
northWestY + size - 1, size);
}


/**
* 3.如果目标在当前棋盘的右下方,则递归求解
*/
if (targetX >= northWestX + size && targetY >= northWestY + size) {
chessBoard(northWestX + size, northWestY + size, targetX, targetY,
size);
} else {
/**
* 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错
*/
if (map[northWestY + size][northWestX + size] != 0) {
System.out
.println("position (" + (northWestX + size - 1) + ", "
+ (northWestY + size - 1)
+ ") has been filled!");
}
/** 重新指定目标代码为左上角方格 */
map[northWestY + size][northWestX + size] = sign;


/** 递归求解当目标代码为左上角方格时的情况 */
chessBoard(northWestX + size, northWestY + size, northWestX + size,
northWestY + size, size);
}


/**
* 4.如果目标在当前棋盘的左下角,则递归求解
*/
if (targetX < northWestX + size && targetY >= northWestY + size) {
chessBoard(northWestX, northWestY + size, targetX, targetY, size);
} else {
/**
* 测试代码:测试该位置是否已经被覆盖,如果已经被覆盖则算法出错
*/
if (map[northWestY + size][northWestX + size - 1] != 0) {
System.out
.println("position (" + (northWestX + size - 1) + ", "
+ (northWestY + size - 1)
+ ") has been filled!");
}
/** 重新指定目标代码为右上角方格 */
map[northWestY + size][northWestX + size - 1] = sign;
/** 递归求解当目标代码为右上角方格时的情况 */
chessBoard(northWestX, northWestY + size, northWestX + size - 1,
northWestY + size, size);
}


// /**
// * 为便于观察让其睡眠2秒
// */
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// /**
// * 打印棋盘
// */
// printMap();
// System.out.println();
}


/**
* 生成地图

* @param size
*/
private void buildMap(int size) {
map = new int[size][size];


/** 初始化数组 */
for (int y = 0; y < size; ++y) {
for (int x = 0; x < size; ++x) {
map[y][x] = 0;
}
}
}


/**
* 打印棋盘
*/
public void printMap() {
for (int y = 0; y < map.length; ++y) {
for (int x = 0; x < map[0].length; ++x) {
System.out.print(map[y][x] + "\t");
}
System.out.println();
}
}


public static void main(String[] args) {


System.out.println("请输入您想要的合法大小,例如:4");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.println("你输入的是:" + i + " * " + i
+ " 的矩阵 , 默认特殊位置为(1, 2),也可以对参数作出更改!");


/**
* 确定参数是否合法
*/
double flag = Math.log(i) / Math.log(2);
if ((flag - (int) flag) != 0) {
System.out.println("参数不合法!");
} else {
Arithmetic arithmetic = new Arithmetic();
arithmetic.chessBoard(0, 0, 1, 2, i);
arithmetic.printMap();
}
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值