/**
 * @version: 
 * @Description:
 * @author: mis001
 * @date: 2020
 */
package com.calculation;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.RowFilter;
/**马踏棋盘算法
 * @Description: 
 * @author Xu
 * @CreateDate:	2020年6月11日
 */
public class Horse {
	private static int x; //列	
	private static int y; //行
	
	//创建一个数组,标记棋盘的每个位置是否被访问
	private static boolean visited[];
	//属性,标记是否棋盘的所有卫士都被访问,如果是true,表示成功
	private static boolean finished ;
	
	public static void main(String[] args) {
		System.out.println("星际争霸龙骑士算法开始游行");
		
		//测试其实周游算法是否正确
		x = 8;
		y = 8;
		int row = 1; //马初始位置的行,从1开始编号
		int column = 1;//初始位置的列,从1开始编号
		
		//创建棋盘
		int [][] chessboard = new int [x][y];
		//初始位置都是false
		visited = new boolean[x * y];
		
		//测试骑士周琦耗时
		long start = System.currentTimeMillis();
		traversalA(chessboard,row - 1,column - 1,1);
		long end = System.currentTimeMillis();
		System.out.println("共耗时: " + (end - start) + " 毫秒");
		
		//输出棋盘最后情况
		for (int[] rows : chessboard) {
			for (int step : rows) {
				System.out.println(step + "\t");
			}
			System.out.println();
		}
	
		
	}
	/**完成骑士周游问题的算法
	 * @param chessboard
	 * @param i
	 * @param j
	 * @param k
	 */
	private static void traversalA(int[][] chessboard, int row, int column, int step) {
		chessboard[row][column] = step;
		//标记位置已经访问过了
		visited[row * x + column] = true;
		
		//获取当前位置可以走下一个位置的集合
		ArrayList<Point> ps = next(new Point(column,row));
		//对ps进行排序,排序的规则就是对ps的所有的Point对象的下一步的位置数目,进行非递减排序
		sort(ps);
		
		//遍历ps
		while (!ps.isEmpty()) {
			//取出下一个可以走的位置
			Point p = ps.remove(0) ;
			//是没有访问过
			if(!visited[p.y * x + p .x]){
				traversalA(chessboard, p.y, p.x, step + 1);
			}
			
			//判断马儿是否完成了任务,使用   step 和应该走的步数比较 , 
			//如果没有达到数量,则表示没有完成任务,将整个棋盘置0
			//说明: step < X * Y  成立的情况有两种
			//1. 棋盘到目前位置,仍然没有走完
			//2. 棋盘处于一个回溯过程
			if(step < x * y && !finished ) {
				chessboard[row][column] = 0;
				visited[row * x + column] = false;
			} else {
				finished = true;
			}
			
		}
		
	}
	
	
	
	/**依据当前的这一步,的所有的下一步的选择位置,进行非递减排序,减少回溯的次数
	 * @param ps
	 */
	private 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();
				//获取到o2的下一步的所有位置个数
				int count2 = next(o2).size();
				
				if (count1 < count2) {
					return -1;
				}else if(count1 == count2){	
					return 0 ;
				}else {
					return 1;
				}
			}
		});
		
	}
	/**
	 * 根据当前位置(Point对象),计算马还能走哪些位置(Point),并放入到一个集合中(ArrayList), 最多有8个位置
	 * @param curPoint
	 * @return
	 */
	public static ArrayList<Point> next(Point curPoint){
		
		//创建ArrayList
		ArrayList<Point> ps = new ArrayList<Point>();
		//创建一个 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());
		}
		
		//判断马可以走1和这个位置
		if ((p1.x = curPoint.x + 2) < x && (p1.y = curPoint.y + 2) < 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) >= 0 && (p1.y = curPoint.y + 2) < y) {
			ps.add(new Point(p1));
		}
		//判断马可以走4这个位置
		if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < y) {
			ps.add(new Point(p1));
		}
		return ps;
		
	}
	
	
}
最近下载更多
                
最近浏览更多
                
                lironggang     LV38
                2023年3月26日
            
            
        
                不停yayaya     LV4
                2021年9月29日
            
            
        
                夏目    
                2021年4月6日
            
            
                    暂无贡献等级
            
        
                廖业贵     LV18
                2021年2月28日
            
            
        
                2196316269     LV10
                2021年2月25日
            
            
        
                走你個魯     LV21
                2020年12月1日
            
            
        
                13043860zj     LV16
                2020年9月24日
            
            
        
                随便取个名字_哈哈     LV27
                2020年9月16日
            
            
        
                Jerry_Handson     LV9
                2020年9月10日
            
            
        
                桌子与灯     LV6
                2020年8月12日
            
            
        
                
                
                
                