洛谷P1002 过河卒(动态规划)

题目描述

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

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

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

输入格式

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

输出格式

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

输入输出样例

输入 
6 6 3 3
输出 
6

说明/提示

对于 100 \%100% 的数据,1 le n, m le 201n,m20,0 le0≤ 马的坐标 le 2020。

1、结果可能会很大,所以用 unsigned long long 

2、每一个点,经过的次数的等于左边和上边点经过次数的和

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
const int f[]={0,1,2,-1,-2,1,2,-1,-2};
const int g[]={0,2,1,2,1,-2,-1,-2,-1};
bool s[30][30];
unsigned long long  a[30][30]; 
int main()
{
  int x,y,m,n;
  cin>>x>>y>>m>>n;
  x++,y++,m++,n++;
  a[1][1]=1;
  s[m][n]=1;
  for(int i=0;i<=8;i++)
     s[m+f[i]][n+g[i]]=1;
  for(int i=1;i<=x;i++)
    for(int j=1;j<=y;j++)
    {
        if(s[i][j]==1)
         continue;
        a[i][j]=max(a[i-1][j]+a[i][j-1],a[i][j]);
        
    }
   cout<<a[x][y]<<endl;
   return 0;
 } 
原文地址:https://www.cnblogs.com/h694879357/p/12285091.html