18.07.01 luoguP1002 过河卒

题目描述

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

棋盘用坐标表示, AA 点 (0, 0)(0,0) 、 BB 点 (n, m)(n,m) ( nn , mm 为不超过 2020 的整数),同样马的位置坐标是需要给出的。

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

输入输出格式

输入格式:

 

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

 

输出格式:

 

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

 

输入输出样例

输入样例#1:
6 6 3 3
输出样例#1: 
6

说明

结果可能很大!

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <math.h>
 5 
 6 using namespace std;
 7 long long ans;
 8 int  xb, yb, xh, yh;
 9 int dir1[2] = { 0,1 }, dir2[2] = { 1,0 };
10 int p1[9] = { 1,-1,2,-2,2,-2,1,-1,0 }, p2[9] = { 2,2,1,1,-1,-1,-2,-2,0 };
11 long long record[25][25];
12 
13 long long dfs(int x,int y) {
14     if (x == xb && y == yb)
15     {
16         return 1;
17     }
18     if (x > xb || y > yb)
19         return 0;
20     if (record[x][y] != 0)
21         return record[x][y];
22     long long tmp = 0;
23     for (int i = 0; i <= 1; i++) {
24         int xx = x + dir1[i], yy = y + dir2[i];
25         bool flag = true;
26         for (int j = 0; j <= 8; j++) {
27             int c1 = xh + p1[j], c2 = yh + p2[j];
28             if (c1 == xx && c2 == yy) {
29                 flag = false;
30                 break;
31             }
32         }
33         if (flag)
34             tmp += dfs(xx, yy);
35     }
36     record[x][y] = tmp;
37     return tmp;
38 }
39 
40 int main()
41 {
42     scanf("%d%d%d%d", &xb, &yb, &xh, &yh);
43     ans=dfs(0, 0);
44     printf("%lld\n", ans);
45     return 0;
46 }
View Code

注意要dp不然要TLE

注意要所有答案相关的数据类型都要设成 long long ,不然会WA

猜你喜欢

转载自www.cnblogs.com/yalphait/p/9249424.html