过河卒_洛谷普及p1002

题目描述

棋盘上 A 点有一个过河卒,需要走到目标 B 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 C 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。

棋盘用坐标表示,A 点 (0,0)、BB 点 (n,m),同样马的位置坐标是需要给出的。

现在要求你计算出卒从 A 点能够到达 B 点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。

输入格式

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

输出格式

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

输入输出样例

输入 

6 6 3 3

输出 

6

说明/提示

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

说明/提示

案例图示如下

展示代码建立了一个m+2, n+2的数组,用于避免马跳不到的地方出现

代码展示

#include <stdio.h>
#include <stdlib.h>
long long go(int n, int m, int nhorse, int mhorse) {
    long long position_horse[n + 3][m + 3];
    long long value[n + 3][m + 3];
    for (int i = 0; i < n + 3; i ++) {
        for (int j = 0; j < m + 3; j++) {
            position_horse[i][j] = 0;
            value[i][j] = 0;
        }
    }
    //定义马的位置, 1
    mhorse += 2;
    nhorse += 2;
    position_horse[nhorse][mhorse] = 1;
    position_horse[nhorse - 1][mhorse - 2] = 1;
    position_horse[nhorse - 1][mhorse + 2] = 1;
    position_horse[nhorse - 2][mhorse - 1] = 1;
    position_horse[nhorse - 2][mhorse + 1] = 1;
    position_horse[nhorse + 1][mhorse - 2] = 1;
    position_horse[nhorse + 1][mhorse + 2] = 1;
    position_horse[nhorse + 2][mhorse - 1] = 1;
    position_horse[nhorse + 2][mhorse + 1] = 1;
    //赋值计算
    value[2][2] = 1;

    for (int i = 1; i < n + 3; i++) {
        for (int j = 1; j < m + 3; j++) {
            while (1) {
                if (position_horse[i][j] == 0) {
                    value[i][j] += value[i][j - 1] + value[i - 1][j];
                }
                break;
            }
        }
    }
    //检查位置和大小
    printf("position of horses\n");
    for (int i = 0; i < n + 3; i ++) {
        for (int j = 0; j < m + 3; j++) {
            printf("%lld ", position_horse[i][j]);
        }
        printf("\n");
    }
    printf("value behind the array\n");
    for (int i = 0; i < n + 3; i ++) {
        for (int j = 0; j < m + 3; j++) {
            printf("%lld ", value[i][j]);
        }
        printf("\n");
    }
    return value[n + 2][m + 2];
}

int main()
{
    int m = 0, n = 0;
    int m_horse = 0, n_horse = 0;
    scanf("%d %d %d %d", &n, &m, &n_horse, &m_horse);
    long long result = go(n, m, n_horse, m_horse);

    printf("%lld", result);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值