题目描述
棋盘上 AAA 点有一个过河卒,需要走到目标 BBB 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 CCC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。
棋盘用坐标表示,AAA 点 (0,0)(0, 0)(0,0)、BBB 点 (n,m)(n, m)(n,m),同样马的位置坐标是需要给出的。
现在要求你计算出卒从 AAA 点能够到达 BBB 点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。 输入格式
一行四个正整数,分别表示 BBB 点坐标和马的坐标。 输出格式
一个整数,表示所有的路径条数。 输入输出样例 输入 #1
6 6 3 3
输出 #1
6
说明/提示
对于 1000 0% 的数据,1≤n,m≤201 \le n, m \le 201≤n,m≤20,0≤0 \le0≤ 马的坐标 ≤20\le 20≤20。
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 66; ll dp[maxn][maxn]; int dx[] = {1, 1, 2, 2, -1, -1, -2, -2}; int dy[] = {2, -2, 1, -1, 2, -2, 1, -1}; int main() { int n, m, x, y; cin >> n >> m >> x >> y; for(int i = 0; i < 8; i++) { int nx = x + dx[i], ny = y + dy[i]; // cout << nx << " " << ny << endl; if(nx >= 0 && ny >= 0) dp[nx][ny] = -1; } dp[x][y] = -1; dp[0][0] = 1; for(int i = 1; i <= m; i++) if(dp[0][i] != -1) dp[0][i] = max(0ll, dp[0][i - 1]); for(int i = 1; i <= n; i++) if(dp[i][0] != -1) dp[i][0] = max(0ll, dp[i - 1][0]); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(dp[i][j] != -1) dp[i][j] = max(dp[i][j], max(0ll, dp[i - 1][j]) + max(0ll, dp[i][j - 1])); } } cout << dp[n][m] << endl; return 0; }