Chinaunix首页 | 论坛 | 博客
  • 博客访问: 321032
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 179
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-09 18:16
文章分类

全部博文(15)

文章存档

2019年(1)

2018年(1)

2015年(7)

2013年(6)

我的朋友

分类: Java

2013-09-11 16:48:41


点击(此处)折叠或打开

  1. package basic;

  2. public class Knight {

  3.     /**
  4.      * @param args
  5.      */
  6.     private final static int N = 5;
  7.     // 下一个出口的8个位置的相对坐标
  8.     private static int[][] nextPos = {{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};

  9.     private static int[][] chessBoard = new int[N][N];
  10.     public static void main(String[] args) {
  11.         int posX = 0; //初始位置
  12.         int posY = 0; //初始位置
  13.         int count = 0;
  14.         int nextExit = -1;
  15.         chessBoard[posX][posX] = ++count;
  16.         while (count < N * N) {
  17.             nextExit = nextExit(posX, posY);
  18.             if (nextExit <= -1 ) {
  19.                 System.out.println("no way");
  20.                 printChessBoard();
  21.                 System.exit(1);
  22.             } else {
  23.                 posX += nextPos[nextExit][0];
  24.                 posY += nextPos[nextExit][1];
  25.                 chessBoard[posX][posY] = ++count;
  26.             }
  27.         }
  28.         printChessBoard();
  29.     }
  30.     
  31.     //找到出口最少的下一个出口
  32.     public static int nextExit(int x, int y) {
  33.         int result = -1;
  34.         int nextPosX;
  35.         int nextPosY;
  36.         int minNext = 8;
  37.         for (int i = 0; i < 8; i++) {
  38.             nextPosX = x + nextPos[i][0];
  39.             nextPosY = y + nextPos[i][1];
  40.             if ( isInboard(nextPosX, nextPosY)) {
  41.                 if (chessBoard[nextPosX][nextPosY] == 0) {
  42.                     if (countExit(nextPosX, nextPosY) < minNext) {
  43.                         minNext = countExit(nextPosX, nextPosY);
  44.                         result = i;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         return result;
  50.     
  51.     }
  52.     
  53.     //计算出口数量
  54.     public static int countExit(int x, int y){
  55.         int count = -1;
  56.         for (int i = 0; i < 8; i++) {
  57.             if (isInboard(x + nextPos[i][0], y + nextPos[i][1])) {
  58.                 if (chessBoard[x + nextPos[i][0]][y + nextPos[i][1]] == 0) {
  59.                     count++;
  60.                 }
  61.             }
  62.         }
  63.         return count;
  64.     }
  65.     
  66.     //计算位置是否在棋盘内
  67.     public static boolean isInboard(int posX, int posY) {
  68.         return 0 <= posX && posX < N && 0 <= posY && posY < N;
  69.     }
  70.     
  71.     //打印结果
  72.     public static void printChessBoard() {
  73.         for (int i = 0; i < N; i++) {
  74.             for (int j = 0; j < N; j++) {
  75.                 System.out.print(chessBoard[i][j] + "\t");
  76.             }
  77.             System.out.println();
  78.         }
  79.     }

  80. }


阅读(1334) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~