PanheadGG
PanheadGG
发布于 2025-12-11 / 66 阅读

数据结构实验二:数组实验

题目

国际象棋由8*8的格子组成,假设棋盘格子编号从1开始,某个马所在位置为(x,y),试编写程序实现马踏遍所有格子的程序,根据顺序列出所有下一步的格子坐标,如(x,y),(x+2,y+1)...

代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class HorseProblem {
    static class Pos {
        int x, y;

        Pos(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return "(" + x + ", " + y + ")";
        }
    }

    static int[][] board = new int[8][8]; // 棋盘,数字表示顺序
    static Stack<Pos> path = new Stack<>(); // 存储马的行走路径

    // 马的8个走向
    static int[] dx = {-2, -2, -1, -1, 1, 1, 2, 2};
    static int[] dy = {-1, 1, -2, 2, -2, 2, -1, 1};

    // 判断位置是否可走
    static boolean isValid(int x, int y) {
        return x >= 0 && x < 8 && y >= 0 && y < 8 && board[x][y] == 0;
    }

    // 当前位置可走的下一步数量
    static int getValidStepCount(int x, int y) {
        int count = 0;
        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (isValid(nx, ny)) count++;
        }
        return count;
    }

    // 递归求解马的行走路径
    static boolean nextStep(int currentX, int currentY, int stepIndex) {
        board[currentX][currentY] = stepIndex;
        path.push(new Pos(currentX + 1, currentY + 1));

        if (stepIndex == 64) return true;
        List<Integer> nextSteps = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            int nx = currentX + dx[i];
            int ny = currentY + dy[i];
            if (isValid(nx, ny)) nextSteps.add(i);
        }

        // 按可走下一步数量排序,Warnsdorff规则
        nextSteps.sort((a, b) -> {
            int countA = getValidStepCount(currentX + dx[a], currentY + dy[a]);
            int countB = getValidStepCount(currentX + dx[b], currentY + dy[b]);
            return Integer.compare(countA, countB);
        });

        // 按排序后的顺序走下一步
        for (int step : nextSteps) {
            int nx = currentX + dx[step];
            int ny = currentY + dy[step];
            if (nextStep(nx, ny, stepIndex + 1)) return true;
        }

        board[currentX][currentY] = 0;
        path.pop();
        return false;
    }

    public static void main(String[] args) {
        System.out.print("请输入马的初始位置(1-8,1-8):");
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt() - 1;
        int y = scanner.nextInt() - 1;
        scanner.close();
        if (!isValid(x, y)) {
            System.out.println("初始位置不合法");
            return;
        }
        if (nextStep(x, y, 1)) {
            System.out.println("马的行走路径为:");
            for(int i = 0; i < 64; i++) {
                System.out.print(path.get(i)+", ");
                if(i%8 == 7) System.out.println();
            }

            System.out.println("\n棋盘状态(数字代表第几步): ");
            for(int i = 0; i < 8; i++){
                for(int j = 0; j < 8; j++){
                    System.out.printf("%4d ",board[i][j]);
                }
                System.out.println();
            }
        } else {
            System.out.println("无解");
        }
    }
}

输入输出实例

请输入马的初始位置(1-8,1-8):5 5
马的行走路径为:
(5, 5), (4, 7), (2, 8), (1, 6), (2, 4), (1, 2), (3, 1), (2, 3), 
(1, 1), (3, 2), (1, 3), (2, 1), (4, 2), (6, 1), (8, 2), (7, 4), 
(8, 6), (7, 8), (5, 7), (3, 8), (1, 7), (3, 6), (1, 5), (2, 7), 
(4, 8), (6, 7), (8, 8), (7, 6), (6, 8), (8, 7), (6, 6), (5, 8), 
(7, 7), (8, 5), (7, 3), (8, 1), (6, 2), (4, 1), (2, 2), (1, 4), 
(2, 6), (1, 8), (3, 7), (2, 5), (3, 3), (4, 5), (5, 3), (3, 4), 
(4, 6), (5, 4), (3, 5), (4, 3), (5, 1), (7, 2), (8, 4), (6, 5), 
(4, 4), (5, 2), (6, 4), (5, 6), (7, 5), (6, 3), (7, 1), (8, 3), 


评论