洛谷刷题集——P1002

题目描述

在这里插入图片描述

输入格式

一行四个正整数,分别表示 B 点坐标和马的坐标。

输出格式

一个整数,表示所有的路径条数。

输入输出样例

输入
6 6 3 3
输出
6

说明/提示

对于 100% 的数据,1≤n,m≤20,0≤ 马的坐标 ≤20。

解题思路

使用递推

代码

import java.util.Scanner;

    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();
            //记录马的位置以及马控制的位置
            int[][] array1 = new int[x1 + 1][y1 + 1];
            long[][] array2 = new long[x1 + 1][y1 + 1];
            array1[x2][y2] = -1;
            for (int i = 0; i <= x1; i++) {
                for (int j = 0; j <= y1; j++) {
                    if ((Math.abs((i - x2)) == 1 && Math.abs((j - y2)) == 2) || Math.abs((i - x2)) == 2 && Math.abs((j - y2)) == 1) {
                        array1[i][j] = -1;
                    }
                }
            }
            //递推
            for (int i = 0; i <= x1; i++) {
                for (int j = 0; j <= y1; j++) {
                    if (array1[i][j] != -1) {
                        if (i == 0 && j == 0) {
                            array2[i][j] = 1;
                        }
                        if (i > 0 && j > 0) {
                            array2[i][j] = array2[i - 1][j] + array2[i][j - 1];
                        }
                        if (i == 0 && j > 0) {
                            array2[i][j] = array2[i][j - 1];
                        }
                        if (j == 0 && i > 0) {
                            array2[i][j] = array2[i - 1][j];
                        }
                    } else {
                        array2[i][j] = 0;
                    }
                }
            }
            System.out.println(array2[x1][y1]);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值