骑士走棋盘问题

骑士走棋盘问题 国际象棋中骑士从棋盘任意位置出发一直将棋盘的每一个位置走一遍

这是国际象棋中骑士的可能走法,黑色表示骑士所在位置,红色表示骑士下一步能走的位置

骑士走棋盘问题 中 先走最难得位置(下一步可能性最少的位置),然后走容易的位置这样比较容易遍历整个棋盘

  1 public class KnightAndBoard {
  2     public static class Step {
  3         int x, y; // 横坐标 纵坐标
  4 
  5         public Step(int x, int y) {
  6             this.x = x;
  7             this.y = y;
  8         }
  9     }
 10 
 11     /**
 12      * 骑士遍历棋盘
 13      * 
 14      * @param start
 15      *            骑士第一步所在的坐标
 16      * @return
 17      */
 18     public static int[][] travelBoard(Step start) {
 19         int[][] board = new int[8][8]; // 初始化为0
 20 
 21         board[start.x][start.y] = 1;
 22         Step currentStep = start;
 23 
 24         for (int i = 2; i < 65; i++) {
 25             List<Step> possibleSteps = possibleSteps(currentStep, board);
 26             // 如果该坐标所有下一步都不能走
 27             if (possibleSteps.size() == 0)
 28                 break;
 29             // 如果该坐标下一步只有一种可能性
 30             else if (possibleSteps.size() == 1) {
 31                 currentStep = possibleSteps.get(0);
 32             }
 33             // 如果该坐标的下一步有多种可能性,那么就找出最复杂的一种可能性
 34             else {
 35                 currentStep = hardStep(possibleSteps, board);
 36             }
 37             board[currentStep.x][currentStep.y] = i;
 38         }
 39 
 40         return board;
 41 
 42     }
 43 
 44     /**
 45      * 检查当前坐标所能到达的所有的下一步坐标,并返回下一步坐标list
 46      * 
 47      * @param step
 48      * @return
 49      */
 50     public static List<Step> possibleSteps(Step currentStep, int[][] board) {
 51         // 所有下一步方向集合
 52         int[][] dirs = { { -2, -1 }, { -1, -2 }, { 1, -2 }, { 2, -1 },
 53                 { 2, 1 }, { 1, 2 }, { -1, 2 }, { -2, 1 } };
 54         List<Step> possibleList = new ArrayList<Step>();
 55         for (int[] dir : dirs) {
 56             Step nextStep = new Step(currentStep.x + dir[0], currentStep.y
 57                     + dir[1]);
 58             if (checkNext(board, nextStep))
 59                 possibleList.add(nextStep);
 60         }
 61         return possibleList;
 62     }
 63 
 64     /**
 65      * 检查下一个坐标是否可走
 66      * 
 67      * @param board
 68      * @param nextDir
 69      * @return
 70      */
 71     public static boolean checkNext(int[][] board, Step nextStep) {
 72         return nextStep.x > -1 && nextStep.x < 8 && nextStep.y > -1
 73                 && nextStep.y < 8 && board[nextStep.x][nextStep.y] == 0;
 74     }
 75 
 76     /**
 77      * 先走最难走的一步,也就是先走下一步可能性最少的一步
 78      * 
 79      * @param steps
 80      * @param board
 81      * @return
 82      */
 83     public static Step hardStep(List<Step> steps, int[][] board) {
 84 
 85         int minIndex = 0;
 86         List<Step> minStep = possibleSteps(steps.get(0), board);
 87 
 88         for (int i = 1; i < steps.size(); i++) {
 89             List<Step> nextSteps = possibleSteps(steps.get(i), board);
 90             if (nextSteps.size() < minStep.size()) {
 91                 minIndex = i;
 92                 minStep = nextSteps;
 93             }
 94         }
 95         return steps.get(minIndex);
 96     }
 97 
 98     public static void main(String args[]){
 99         
100         KnightAndBoard knightAndBoard = new KnightAndBoard();
101         Step start = new KnightAndBoard.Step(5, 6);
102         for(int [] row :knightAndBoard.travelBoard(start)){
103             for(int step : row)
104                 System.out.print(step + "\t");
105             System.out.println();
106         }
107     }
108 }

 

posted on 2013-01-29 17:59  JoshuaZhu  阅读(292)  评论(0)    收藏  举报

导航