题目 3038: 马走日

题目描述:

马在中国象棋以日字形规则移动。

请编写一段程序,给定n×m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点。

代码:

package lanqiao;

import java.util.*;

public class Main {
    static int n,m,x,y;
    static int max = 0;
    static int dx[] = {1,1,-2,-1,-2,-1,2,2};
    static int dy[] = {2,-2,1,2,-1,-2,1,-1};
    static boolean f[][];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t -- > 0)
        {
            n = sc.nextInt();
            m = sc.nextInt();
            f = new boolean[15][15];
            x = sc.nextInt();
            y = sc.nextInt();
            max = 0;
            f[x][y] = true;
            dfs(x,y,1);
            System.out.println(max);

        }
    }

    public static void dfs(int x,int y,int sum){
        if(sum == n*m){
            max += 1;
            return;
        }
        for(int i = 0;i < 8;i ++){
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx < 0 || xx >= n || yy >= m || yy < 0 || f[xx][yy]){
                continue;
            }
            f[xx][yy] = true;
            dfs(xx,yy,sum + 1);
            f[xx][yy] = false;
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

几两春秋梦_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值