马兰过河卒
二、设计思想
首先将所有点设置为可通行,将控制点设置为不可通行。因为只可以向下或者向右行走,所以要想到达目标点只需要到达其左边的点或者上边的点,以此类推,计算从起点到达中间任何一个点的路径条数,并用数组tripnum[][]保存到达这个点的路径条数。
三、代码
#include<iostream> #include<cstdio> using namespace std; int main() { int a[9] = { 0,-1,-1,-2,-2,1,1,2,2 }; int b[9] = { 0,2,-2,1,-1,2,-2,1,-1 }; int map[16][16] = { 0 }; long long tripnum[16][16] = { 0 }; int n, m, x, y; scanf_s("%d %d %d %d", &n, &m, &x, &y); for (int i = 0; i < 16; i++) for (int j = 0; j < 16; j++) map[i][j] = 1; for (int i = 0; i < 9; i++) { if (x + a[i] <= 15 && x + a[i] >= 0 && y + b[i] <= 15 && y + b[i] >= 0) map[x + a[i]][y + b[i]] = 0; } for (int i = 0; i < 16; i++) { if (map[0][i] == 1) { tripnum[0][i] = 1; } else break; } for (int i = 0; i < 16; i++) { if (map[i][0] == 1) { tripnum[i][0] = 1; } else break; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (map[i][j] == 1) tripnum[i][j] = tripnum[i - 1][j] + tripnum[i][j - 1]; } } printf("%lld", tripnum[n][m]); return 0; }