|
发表于 2006-6-16 15:31:07
|
显示全部楼层
8x8的棋盘让用户手动寻找几乎不可能
我写了一段用机器自动寻找的,里面是一个递归程序,找到一个路径就返回
CHESS_SIZE是棋盘的大小,CHESS_SIZE为5时表示5x5的
8x8的太大了,时间太久就改成5x5的了
- package game;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.LinkedList;
- class Cordinate {
-
- public int x;
- public int y;
- Cordinate(int cx, int cy) {
- x = cx;
- y = cy;
- }
- public String toString() {
- return "(" + x + "," + y + ")";
- }
- }
- public class Chess {
- public static int CHESS_SIZE = 5;
- public static Cordinate INIT_CORD = new Cordinate(0, 0);
- /**
- * Test whether a cordinate is valid in a chesspane
- * @param cord
- * @param clength
- * @return whether it is valid
- */
- public static boolean valid(int cord, int clength) {
- if ((cord >= 0) && (cord < clength))
- return true;
- else
- return false;
- }
- /**
- * Calculate next steps from the latest step
- * @param chesspane:
- * show which cordinate has been visited and which hasn't
- * @param laststep:
- * the last cordinate which has been visited
- * @return A list of Cordinates which can be visited, excluding the visited
- * Cordinates
- */
- public static ArrayList<Cordinate> nextStep(int[][] chesspane,
- Cordinate laststep) {
- int j, k;
- int nextx, nexty;
- int clength = chesspane.length;
- ArrayList<Cordinate> result = new ArrayList<Cordinate>();
- for (j = -2; j <= 2; j++) {
- if (j == 0)
- continue;
- nextx = laststep.x + j;
- k = Math.abs(j) - 3;
- nexty = laststep.y + k;
- if (valid(nextx, clength) && valid(nexty, clength)
- && chesspane[nextx][nexty] == 0)
- result.add(new Cordinate(nextx, nexty));
- k = 3 - Math.abs(j);
- nexty = laststep.y + k;
- if (valid(nextx, clength) && valid(nexty, clength)
- && chesspane[nextx][nexty] == 0)
- result.add(new Cordinate(nextx, nexty));
- }
- return result;
- }
- /**
- * @param chesspane:
- * show which cordinate has been visited and which hasn't
- * @param path:
- * the path of cordinates where has been visited
- * @return the length of the path, if it equals "chesspane.length *
- * chesspane.length" , means having found a solution.
- */
- public static int chessfrom(int[][] chesspane, LinkedList<Cordinate> path) {
- int pathlength;
- Cordinate laststep = path.getLast();
- ArrayList<Cordinate> nextsteps = nextStep(chesspane, laststep);
- if (nextsteps.size() == 0) {
- if (path.size() == chesspane.length * chesspane.length) {
- System.out.println(path);
- }
- return path.size();
- }
- for (Cordinate step : nextsteps) {
- path.add(step);
- chesspane[step.x][step.y] = 1;
- pathlength = chessfrom(chesspane, path);
- if (pathlength == chesspane.length * chesspane.length) {
- // System.out.println(path);
- return chesspane.length * chesspane.length;
- } else {
- path.removeLast();
- chesspane[step.x][step.y] = 0;
- }
- }
- return 0;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[][] chesspane = new int[CHESS_SIZE][CHESS_SIZE];
- LinkedList<Cordinate> path = new LinkedList<Cordinate>();
- for (int i = 0; i < chesspane.length; i++) {
- Arrays.fill(chesspane[i], 0);
- }
- path.add(INIT_CORD);
- chesspane[INIT_CORD.x][INIT_CORD.y] = 1;
- int length = chessfrom(chesspane, path);
- System.out.println(length);
- }
- }
复制代码 |
|