马拦过河卒

题目

描述 Description
  棋盘上A点有一个过河卒,需要走到目标B点。卒行走的规则:可以向下、或者向右。同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。
  棋盘用坐标表示,A点(0, 0)、B点(n, m)(n, m为不超过15的整数),同样马的位置坐标是需要给出的。现在要求你计算出卒从A点能够到达B点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。
在这里插入图片描述
输入格式 Input Format
一行四个数据,分别表示B点坐标和马的坐标。
输出格式 Output Format
一个数据,表示所有的路径条数。
样例输入 Sample Input
6 6 3 3
样例输出 Sample Output
6

题解

一道二维动态规划题
方程式为
f即为马控制的地方
a[i][j] = a[i - 1][j] + a[i][j - 1];
if (f[i - 1][j - 1]) a[i][j] = 0;
注意::一定要特判因为坐标相减会出现负数
↓ 不特判的后果在这里插入图片描述

code

#include <bits/stdc++.h>
#define MAXX 50
using namespace std;
typedef long long ll;

inline ll read() {
	ll s = 0, w = 1;
	char ch = getchar();
	while(!isdigit(ch)) { if(ch == '-') w = -1; ch = getchar(); }
	while(isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
	return s * w;
}

ll n, m, x, y; 
ll f[MAXX][MAXX], a[MAXX][MAXX];

inline void cover(ll xh,ll yh) {
    f[xh][yh] = 1;
    if((xh - 1 >= 0) && (yh - 2 >= 0)) f[xh - 1][yh - 2] = 1;
    if((xh - 2 >= 0) && (yh - 1 >= 0)) f[xh - 2][yh - 1] = 1;
    if(xh - 2 >= 0) f[xh - 2][yh + 1] = 1;
    if(xh - 1 >= 0) f[xh - 1][yh + 2] = 1;
    if(yh - 2 >= 0) f[xh + 1][yh - 2] = 1;
    if(yh - 1 >= 0) f[xh + 2][yh - 1] = 1;
    f[xh + 2][yh + 1] = 1;
    f[xh + 1][yh + 2] = 1; 
}

int main() {
	n = read(); m = read(); x = read(); y = read();
	cover(x,y);
	a[1][0] = 1;
	for (int i = 1; i <= n + 1; ++i) {
		for (int j = 1; j <= m + 1; ++j) {
			a[i][j] = a[i - 1][j] + a[i][j - 1];
			if (f[i - 1][j - 1]) a[i][j] = 0;
		}
	}
	printf("%lld", a[n + 1][m + 1]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值