洛谷P1002过河卒-题解
原题:
思路:
显然是计数DP
又是在网格上,又是只能走下和右,思路是很显然的
想要到达这个点,只能由这个点上方和左方的点转移来
方程也很明显了:
f[i][j]=f[i-1][j]+f[i][j+1];
注意特判被马占的点就可以了
代码:
#include <bits/stdc++.h> using namespace std; int n, m, x, y; unsigned long long f[25][25]; int book[25][25]; int fx[] = {0, -2, -1, 1, 2, 2, 1, -1, -2}; int fy[] = {0, 1, 2, 2, 1, -1, -2, -2, -1}; int main() { scanf("%d %d %d %d", &n, &m, &x, &y); n++; m++; x++; y++; book[x][y] = 1; f[1][1] = 1; for (int i = 1; i <= 8; i++) book[x + fx[i]][y + fy[i]] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (book[i][j] == 0) f[i][j] = max(f[i - 1][j] + f[i][j - 1], f[i][j]); printf("%llu\n", f[n][m]); return 0; }