java实现马踏棋盘算法(骑士周游问题)

网友投稿 276 2022-08-28


java实现马踏棋盘算法(骑士周游问题)

骑士周游问题

在8x8的国际棋盘上,按照马走日的规则,验证是否能够走遍棋盘。

解题思路

1、创建棋盘 chessBoard,是一个二维数组。2、将当前位置设置为已经访问,然后根据当前位置,计算马儿还能走哪些位置,并放入到一个集合中(ArrayList),最多有8个位置,每走一步,就使用step+1。3、遍历ArrayList中存放的所有位置,看看哪个可以走通,如果走通,就继续,走不通,就回溯。4、判断马儿是否完成了任务,使用step和应该走的步数比较,如果没有达到数量,则表示没有完成任务,将整个棋盘置0。5、注意:马儿不同的走法(策略),会得到不同的结果,效率也会有影响(优化)。

使用贪心算法优化

1、我们获取当前位置,可有走的下一个位置的集合ArrayList ps = next(new Point(column, row));2、我们需要对ps中所有的Point的下一步的所有集合的数目,进行非递减排序。

优化代码

public static void sort(ArrayList ps) {

ps.sort(new Comparator() {

@Override

public int compare(Point o1, Point o2) {

// 获取o1的下一步的所有位置的个数

int count1 = next(o1).size();

int count2 = next(o2).size();

if (count1 < count2) {

return -1;

} else if (count1 == count2) {

return 0;

} else {

return 1;

}

}

});

}

马踏棋盘算法代码实现

package com.horse;

import java.awt.Point;

import java.util.ArrayList;

import java.util.Comparator;

public class HorseChessboard {

private static int X;// 棋盘的列数

private static int Y;// 棋盘的行数

private static boolean visited[]; // 标记棋盘的位置是否被访问过

private static boolean finished;// 标记棋盘的所有位置都被访问(是否成功)

public static void main(String[] args) {

// 测试http://骑士周游算法

X = 8;

Y = 8;

int row = 1;// 马儿的初始位置行

int column = 1;// 马儿初始位置列

// 创建棋盘

int[][] chessboard = new int[X][Y];

visited = new boolean[X * Y];

// 测试一下耗时

long start = System.currentTimeMillis();

traversalChessboard(chessboard, row - 1, column - 1, 1);

long end = System.currentTimeMillis();

System.out.println("耗时" + (end - start) + "ms");

// 输出棋盘最后情况

for (int[] rows : chessboard) {

for (int step : rows) {

System.out.printf("%4d", step);

}

System.out.println();

}

}

/**

* @Method_Name:traversalChessboard

* @Description: 完成骑士周游问题多的算法

* @param chessboard

* 棋盘

* @param row

* 马儿当前位置的行 从0开始

* @param column

* 马儿当前位置的列 从0开始

* @param step

* void 是第几步,初始位置是第1步

*/

public static void traversalChessboard(int[][] chessboard, int row, int column, int step) {

chessboard[row][column] = step;

visited[row * X + column] = true;// 标记该位置已访问

// 获取当前位置可以走的下一步

ArrayList ps = next(new Point(column, row));

// 对ps进行非递减排序,

sort(ps);

// 遍历ps

while (!ps.isEmpty()) {

Point p = ps.remove(0);// 取出下一个可以走的位置

// 判断是否访问过

if (!visited[p.y * X + p.x]) {// 说明还没有访问过

traversalChessboard(chessboard, p.y, p.x, step + 1);

}

}

// 判断是否完成

if (step < X * Y && !finished) {

chessboard[row][column] = 0;

visited[row * X + column] = false;

} else {

finished = true;

}

}

/**

* @Method_Name:next

* @Description: 计算马儿还能走哪些位置,并放入到一个集合中(ArrayList)

* @param curPoint

* @retddUaCYeHfaurn ArrayList

*/

public static ArrayList next(Point curPoint) {

// 创建有一个ArrayList

ArrayList ps = new ArrayList();

// 创建Point

Point p1 = new Point();

// 判断马儿可以走5这个位置

if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {

ps.add(new Point(p1));

}

// 判断马儿可以走6这个位置

if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {

ps.add(new Point(p1));

}

// 判断马儿可以走7这个位置

if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {

ps.add(new Point(p1));

}

// 判断马儿可以走0这个位置

if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {

ps.add(new Point(p1));

}

// 判断马儿可以走1这个位置

if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {

ps.add(new Point(p1));

}

// 判断马儿可以走2这个位置

if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {

ps.add(new Point(p1));

}

// 判断马儿可以走3这个位置

if ((p1.x = curPoint.x - 1) >=http:// 0 && (p1.y = curPoint.y + 2) < Y) {

ps.add(new Point(p1));

}

// 判断马儿可以走4这个位置

if ((p1.x = ddUaCYeHfacurPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {

ps.add(new Point(p1));

}

return ps;

}

// 根据当前这个一步的所有的下一步的选择位置,进行非递减排序

public static void sort(ArrayList ps) {

ps.sort(new Comparator() {

@Override

public int compare(Point o1, Point o2) {

// 获取o1的下一步的所有位置的个数

int count1 = next(o1).size();

int count2 = next(o2).size();

if (count1 < count2) {

return -1;

} else if (count1 == count2) {

return 0;

} else {

return 1;

}

}

});

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:pytest参数化使用ids当测试用例描述有中文时控制台及报告中用例标题输出为Unicode编码问题(钩子函数:pytest_collection_modifyitems)
下一篇:pytest参数化自定义测试用例标题【@pytest.mark.parametrize(ids=XXX)】(pytest标签)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~