【洛谷】P1002 过河卒

  • 题目链接
    P1002 过河卒

  • 思路
    ①棋盘中一点走法为该点上方点走法+该点左侧点走法,即

    way[x][y] = way[x - 1][y] + way[x][y - 1]

    ②对方马的控制点走法为0

  • 样例棋盘

    1 1 1 1 1 1 1
    1 2 x 1 x 1 2
    1 x 0 1 1 x 2
    1 1 1 x 1 1 3
    1 x 1 1 2 x 3
    1 1 x 1 x 0 3
    1 2 2 3 3 3 6

  • 代码

#include<stdio.h>

unsigned long long bd[21][21];
int h[9][2];

void horsepoint() {
	h[4][0] = h[5][0] = h[0][0] - 2;
	h[3][0] = h[6][0] = h[0][0] - 1;
	h[2][0] = h[7][0] = h[0][0] + 1;
	h[1][0] = h[8][0] = h[0][0] + 2;
	h[6][1] = h[7][1] = h[0][1] - 2;
	h[5][1] = h[8][1] = h[0][1] - 1;
	h[1][1] = h[4][1] = h[0][1] + 1;
	h[2][1] = h[3][1] = h[0][1] + 2;
}//对方马的控制点

int horse(int x, int y) {
	for (int i = 0; i < 9; i++)
		if (x == h[i][0] && y == h[i][1])
			return 1;
	return 0;
}//判断该点是否为马的控制点

int main()
{
	int bx, by;
	scanf("%d%d%d%d", &bx, &by, &h[0][0], &h[0][1]);
	horsepoint();
	for (int x = 0; x <= bx; x++)
		for (int y = 0; y <= by; y++)
			bd[x][y] = 0;
	bd[0][0] = 1;//(0,0)点初始为一种走法
	for (int x = 0; x <= bx; x++)
		for (int y = 0; y <= by; y++)
		{
			if (x == 0 && y == 0)
				continue;
			if (horse(x, y))//跳过马点
				continue;
			if (x)
				bd[x][y] += bd[x - 1][y];//累加上方点走法
			if (y)
				bd[x][y] += bd[x][y - 1];//累加左侧点走法
		}
	/*for (int x = 0; x <= bx; x++)
	{
		for (int y = 0; y <= by; y++)
			printf("%llu ", bd[x][y]);
		puts("");
	}*/
	printf("%llu", bd[bx][by]);
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值