dp , 记忆化搜索
状态转移方程
d
p
(
x
,
y
)
=
d
p
(
x
−
1
,
y
)
+
d
p
(
x
,
y
−
1
)
dp(x, y) = dp(x-1, y) + dp(x, y-1)
dp(x,y)=dp(x−1,y)+dp(x,y−1)
标记 9 个马所控制的点(一开始以为只有8个,导致样例没过)
AC代码
#include <iostream>
using namespace std;
using LL = long long;
LL dp[30][30];
bool vis[30][30];
LL gdp(LL x, LL y) {
if (!vis[x][y]) {
vis[x][y] = true;
if (x - 1 >= 0)
dp[x][y] += gdp(x-1, y);
if (y - 1 >= 0)
dp[x][y] += gdp(x, y-1);
}
// cout << "dp(" << x << " ," << y << ") = " << dp[x][y] << endl;
return dp[x][y];
}
int main()
{
for (LL i = 0; i < 30; ++i) {
for (LL j = 0; j < 30; ++j) {
vis[i][j] = false;
dp[i][j] = 0;
}
}
dp[0][0] = 1;
vis[0][0] = true;
LL n, m, hx, hy;
cin >> n >> m >> hx >> hy;
vis[hx][hy] = true;
vis[hx+1][hy+2] = true;
vis[hx+2][hy+1] = true;
if (hx >= 1) {
vis[hx-1][hy+2] = true;
if (hy >= 2) {
vis[hx-1][hy-2] = true;
}
if (hx >= 2) {
vis[hx-2][hy+1] = true;
if (hy >= 1) {
vis[hx-2][hy-1] = true;
}
}
}
if (hy >= 1) {
vis[hx+2][hy-1] = true;
if (hy >= 2) {
vis[hx+1][hy-2] = true;
}
}
cout << gdp(n, m) << endl;
// for (int i = 0; i <= n; ++i) {
// for (int j = 0; j <= n; ++j) {
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
return 0;
}