NOIP 2002 过河卒【记忆化搜索】

NOIP 2002 过河卒

我大概是思维僵化了,我一直就在那里琢磨怎么怎么DP,然后。。。就忘记了记忆化搜索= =,太呆了QAQ

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long int
#define LL ll
#define INF 0x3f3f3f3f
const int maxn = 25;
int n, m, x, y;
int ex, ey;
LL dp[25][25];//答案存在爆int的可能
int a[25][25];
int dx[2] = { 0,1 };
int dy[2] = { 1,0 };
int check(int x,int y,int cx,int cy) {//判断(x,y)是否在马的可控制区域内。
	int dx = abs(cx - x);
	int dy = abs(cy - y);
	if (dx > dy) swap(dx, dy);
	if (dx == 1 && dy == 2) return 1;
	else return 0;
}
LL dfs(int x, int y) {//记忆化搜索
	if (dp[x][y]) return dp[x][y];
	if (x == ex && y == ey) return 1;
	int tx, ty;
	for (int i = 0; i <= 1; i++) {
		tx = x + dx[i];
		ty = y + dy[i];
		if (a[tx][ty] == 0) {
			dp[x][y] += dfs(tx, ty);
		}
	}
	return dp[x][y];
}

int main() {
	scanf("%d %d %d %d", &n, &m, &x, &y);
	ex = n + 1; ey = m + 1;
	for (int i = 0; i <= n + 2; i++) {//将地图整体向右下平移一个单位
		for (int j = 0; j <= m + 2; j++) {
			a[i][j] = 1;
		}
	}
	for (int i = 1; i <= n + 1; i++) {
		for (int j = 1; j <= m + 1; j++) {//马所能控制的点设置为墙
			if (check(i, j, x + 1, y + 1)) {
				a[i][j] = 1;
			}
			else a[i][j] = 0;
		}
	}
	a[x + 1][y + 1] = 1;//C点设置为墙
	LL ans = dfs(1, 1);
	printf("%lld\n", ans);
	return 0;
}
//_CRT_SECURE_NO_WARNINGS


你可能感兴趣的:(Algorithm)