设dp[i][j]为从[0,0]到[i,j]的方案数,那么dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 这里i - 1 和j - 1必须在边界里并且[i - 1, j]和[i, j - 1]必须不能被马控制.
用的是记忆化搜索.
#include <cstdio>
#include <memory.h>
using namespace std;
const int MAX = 20;
bool reigned[MAX][MAX];
long long dp[MAX][MAX];
int dir[8][2] = {{-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2}, {-2, 1}};
int ei, ej, hi, hj;
void init(int i, int j){
reigned[i][j] = true;
for(int k = 0; k < 8; ++k){
int hi = i + dir[k][0], hj = j + dir[k][1];
if(hi >= 0 && hi <= ei && hj >= 0 && hj < ej){
reigned[hi][hj] = true;
}
}
}
long long memoized_dfs(int i, int j){
if(reigned[i][j])return 0;
else if(dp[i][j] != -1)return dp[i][j];
else if(i == 0 && j == 0)return 1;
dp[i][j] = 0;
if(i - 1 >= 0){
dp[i][j] += memoized_dfs(i - 1, j);
}
if(j - 1 >= 0){
dp[i][j] += memoized_dfs(i, j - 1);
}
return dp[i][j];
}
int main(int argc, char const *argv[]){
scanf("%d%d%d%d", &ei, &ej, &hi, &hj);
init(hi, hj);
memset(dp, -1, sizeof(dp));
printf("%lld\n", memoized_dfs(ei, ej));
return 0;
}