马走日棋盘算法

马的遍历问题。在N*M的棋盘中,马只能走日字。马从位置(x,y)处出发,把
棋盘的每一格都走一次,且只走一次。找出所有路径。
输入 N M x y
如 5 5 3 3
5*5的棋盘 马从第3行第3列开始出发


import java.util.Scanner;

public class Main {
    private static int[][] next = new int[][] { { 1, 2 }, { 1, -2 }, { -1, 2 }, { -1, -2 }, { 2, 1 }, { 2, -1 },
            { -2, 1 }, { -2, -1 }, };
    private static int[][] book;
    private static int[][] map;
    private static int[][] a;//用来记录路径信息
    private static int n, m;
    private static int count = 0;//统计有多少种走法

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        int x = sc.nextInt() - 1;
        int y = sc.nextInt() - 1;
        map = new int[n][m];
        book = new int[n][m];
        a = new int[n][m];
        book[x][y] = 1;
        dfs(x, y, 1);
        System.out.println(count);
    }

    private static void dfs(int x, int y, int step) {
        // TODO Auto-generated method stub
        a[x][y] = step;
        if (step == n * m) {
            sop(a);
            count++;
        }
        int tx = 0, ty = 0;
        for (int i = 0; i < 8; i++) {
            tx = x + next[i][0];
            ty = y + next[i][1];
            if (tx < 0 || tx >= n || ty < 0 || ty >= m) {
                continue;
            }
            if (book[tx][ty] == 0) {
                book[tx][ty] = 1;
                dfs(tx, ty, step + 1);
                book[tx][ty] = 0;
            }
        }

    }

    private static void sop(int[][] arr) {
        // TODO Auto-generated method stub
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(arr[i][j] + "  ");
            }
            System.out.println();
        }
        System.out.println("---------");
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值