LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1425|回复: 1

骑士巡游问题

[复制链接]
发表于 2006-6-1 18:05:13 | 显示全部楼层 |阅读模式
根据骑士巡游问题(就是8*8国际象棋盘上马从任意位置开始走遍棋盘每个位置 并且每个位置只能走一次)用JAVA编写一小游戏,要求用户手动寻找路径。急啊
有高手吗 帮忙弄个哦 十分着急 如果哪位高手帮忙请联系我哦 (重金答谢)QQ19778847
发表于 2006-6-16 15:31:07 | 显示全部楼层

8x8的棋盘让用户手动寻找几乎不可能

我写了一段用机器自动寻找的,里面是一个递归程序,找到一个路径就返回
CHESS_SIZE是棋盘的大小,CHESS_SIZE为5时表示5x5的
8x8的太大了,时间太久就改成5x5的了


  1. package game;

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.LinkedList;

  5. class Cordinate {
  6.        
  7.         public int x;

  8.         public int y;

  9.         Cordinate(int cx, int cy) {
  10.                 x = cx;
  11.                 y = cy;
  12.         }

  13.         public String toString() {
  14.                 return "(" + x + "," + y + ")";
  15.         }
  16. }

  17. public class Chess {

  18.         public static int CHESS_SIZE = 5;

  19.         public static Cordinate INIT_CORD = new Cordinate(0, 0);

  20.         /**
  21.          * Test whether a cordinate is valid in a chesspane
  22.          * @param cord
  23.          * @param clength
  24.          * @return whether it is valid
  25.          */
  26.         public static boolean valid(int cord, int clength) {
  27.                 if ((cord >= 0) && (cord < clength))
  28.                         return true;
  29.                 else
  30.                         return false;
  31.         }

  32.         /**
  33.          * Calculate next steps from the latest step
  34.          * @param chesspane:
  35.          *            show which cordinate has been visited and which hasn't
  36.          * @param laststep:
  37.          *            the last cordinate which has been visited
  38.          * @return A list of Cordinates which can be visited, excluding the visited
  39.          *         Cordinates
  40.          */
  41.         public static ArrayList<Cordinate> nextStep(int[][] chesspane,
  42.                         Cordinate laststep) {
  43.                 int j, k;
  44.                 int nextx, nexty;
  45.                 int clength = chesspane.length;
  46.                 ArrayList<Cordinate> result = new ArrayList<Cordinate>();

  47.                 for (j = -2; j <= 2; j++) {
  48.                         if (j == 0)
  49.                                 continue;

  50.                         nextx = laststep.x + j;

  51.                         k = Math.abs(j) - 3;
  52.                         nexty = laststep.y + k;
  53.                         if (valid(nextx, clength) && valid(nexty, clength)
  54.                                         && chesspane[nextx][nexty] == 0)
  55.                                 result.add(new Cordinate(nextx, nexty));
  56.                         k = 3 - Math.abs(j);
  57.                         nexty = laststep.y + k;
  58.                         if (valid(nextx, clength) && valid(nexty, clength)
  59.                                         && chesspane[nextx][nexty] == 0)
  60.                                 result.add(new Cordinate(nextx, nexty));
  61.                 }

  62.                 return result;
  63.         }

  64.         /**
  65.          * @param chesspane:
  66.          *            show which cordinate has been visited and which hasn't
  67.          * @param path:
  68.          *            the path of cordinates where has been visited
  69.          * @return the length of the path, if it equals "chesspane.length *
  70.          *         chesspane.length" , means having found a solution.
  71.          */
  72.         public static int chessfrom(int[][] chesspane, LinkedList<Cordinate> path) {
  73.                 int pathlength;
  74.                 Cordinate laststep = path.getLast();
  75.                 ArrayList<Cordinate> nextsteps = nextStep(chesspane, laststep);

  76.                 if (nextsteps.size() == 0) {
  77.                         if (path.size() == chesspane.length * chesspane.length) {
  78.                                 System.out.println(path);
  79.                         }
  80.                         return path.size();
  81.                 }

  82.                 for (Cordinate step : nextsteps) {
  83.                         path.add(step);
  84.                         chesspane[step.x][step.y] = 1;
  85.                         pathlength = chessfrom(chesspane, path);

  86.                         if (pathlength == chesspane.length * chesspane.length) {
  87.                                 // System.out.println(path);
  88.                                 return chesspane.length * chesspane.length;
  89.                         } else {
  90.                                 path.removeLast();
  91.                                 chesspane[step.x][step.y] = 0;
  92.                         }
  93.                 }
  94.                 return 0;
  95.         }

  96.         /**
  97.          * @param args
  98.          */
  99.         public static void main(String[] args) {
  100.                 // TODO Auto-generated method stub
  101.                 int[][] chesspane = new int[CHESS_SIZE][CHESS_SIZE];
  102.                 LinkedList<Cordinate> path = new LinkedList<Cordinate>();

  103.                 for (int i = 0; i < chesspane.length; i++) {
  104.                         Arrays.fill(chesspane[i], 0);
  105.                 }

  106.                 path.add(INIT_CORD);
  107.                 chesspane[INIT_CORD.x][INIT_CORD.y] = 1;

  108.                 int length = chessfrom(chesspane, path);
  109.                 System.out.println(length);
  110.         }

  111. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表