马踏棋 骑士周游问题与优化

马踏棋盘算法介绍和游戏演示

1)马踏棋盘算法也被称为骑士周游问题
2)将马随机放在国际象棋的8X8棋盘Board[0~ 7][0~7]的某个方格中,马按走棋规则(马走日字)进行移动。要求每个方格只进入一次,走遍棋盘上全部64个方格
在这里插入图片描述
马踏棋盘游戏代码实现

1)马踏棋盘问题(骑士周游问题)实际上是图的深度优先搜索(DFS)的应用。
2)如果使用回溯(就是深度优先搜索)来解决,假如马儿踏了53个点,如图:走到了第53个,坐标(1,0) ,发现已经走到尽头,没办法,那就只能回退了,查看其他的路径,就在棋盘上不停的回溯… 思路分析+代码实现
3)分析第一种方式的问题,并使用贪心算法(greedyalgorithm) 进行优化。解决马踏棋盘问题.

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

//创建一个Point
Point p1 = new Point();
if((p1.x= curPoint.x- 2) >= 0 && (p1.y= curPoint.y-1)>= 0){
ps.add(new Point(p1);

使用贪心算法对原来的算法优化
1。我们获取当前位置,可以走的下一个位置的集合.
//获取当前位置可以走的下一个位置的集合
ArrayList ps = next(new Point(column, row));
2.我们需要对ps中所有的Point的下一步的所有集合的数目,进行非递减排序就ok,
9,7,6,5,3,2,1//递馘排序
1,2,3,4,5,6,10, //递增排序
1,2,2, 2,3,3, 4,5, 6//非递臧
9, 7, 6, 6,6,5, 5,3,2,1 //非递增


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) {
		x = 8;
		y = 8;
		int row = 4;//马初始位置从1开始编号
		int column = 2;
		int[][] chessboard = new int[x][y];
		visited = new boolean[x*y];//初始值都为false
		
		System.out.println(System.currentTimeMillis()/1000);
		traversal(chessboard,row - 1,column - 1,1);

		System.out.println(System.currentTimeMillis()/1000);
		for(int[] rows : chessboard) {
			for(int step : rows) {
				System.out.print(step+" ");
		
			}
			System.out.println();
		}
		
	}
	
	
	//根据当前位置(point) 计算马还能走哪些位置(point) 并放入集合中(最多能走的位置是八个)
	public static ArrayList<Point> next(Point curPoint){
		//Point类 代表点
		ArrayList<Point> ps = new ArrayList<Point>();
		Point p1 = new Point();
		if((p1.x = curPoint.x - 2) >= 0 && ( p1.y = curPoint.y - 1) >= 0) {
			//往左上走横日
			ps.add(new Point(p1));//重新创建匿名对象加入
		}
		if((p1.x = curPoint.x - 1) >= 0 && ( p1.y = curPoint.y - 2) >= 0) {
			//往左上走竖日
			ps.add(new Point(p1));
		}
		if((p1.x = curPoint.x - 2) >= 0 && ( p1.y = curPoint.y + 1) < y) {
			//往左下走横日
			ps.add(new Point(p1));
		}
		if((p1.x = curPoint.x - 1) >= 0 && ( p1.y = curPoint.y + 2) < y) {
			//往左下走竖日
			ps.add(new Point(p1));
		}
		if((p1.x = curPoint.x + 2) < x && ( p1.y = curPoint.y + 1) < y) {
			//往右下走横日
			ps.add(new Point(p1));
		}
		if((p1.x = curPoint.x + 1) < x && ( p1.y = curPoint.y + 2) < y) {
			//往右下走竖日
			ps.add(new Point(p1));
		}
		if((p1.x = curPoint.x + 2) < x && ( p1.y = curPoint.y - 1) >= 0) {
			//往右上走横日
			ps.add(new Point(p1));
		}
		if((p1.x = curPoint.x + 1) < x && ( p1.y = curPoint.y - 2) >= 0) {
			//往右上走竖日
			ps.add(new Point(p1));
		}
		return ps;
	}
	
	//优化
	//根据当前这一步的所有下一步的选择位置 进行非递减排序 减少回溯次数
	public static void sort(ArrayList<Point> ps) {
		ps.sort(new Comparator<Point>() {

			@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;
				}
			}
			
		});
	}
	
	//Chessboard棋盘 row行 column列(都从零开始) step走了第几步
	public static void traversal(int[][] chessboard,int row,int column,int step) {
		chessboard[row][column] = step;
		//标记为以走过
		visited[row * x + column] = true;//[row * x + column]表示在棋盘的第几个位置 横着一行行数(从0开始)
		//获取当前位置可走的下个位置的集合
		ArrayList<Point> ps = next(new Point(column,row));
		//对ps所有Point对象的下一步骤的数目进行排序
		sort(ps);		
		while(!ps.isEmpty()) {
			Point p = ps.remove(0);//取出下一个走的位置
			if(!visited[p.y * x + p.x ]) {//改点未访问过
				traversal(chessboard,p.y,p.x,step+1);
				
			}
		}
		//step < x * y有两种情况
		//1.棋盘到当前位置仍没有走完
		//2.棋盘处于下一个回溯过程 完成一次后避免之前的回溯
		if(step < x * y && !finished) {
			//说明while结束还没有走完  进行回溯
			chessboard[row][column] = 0;
			visited[row*x + column] = false;
		}else {
			finished = true;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值