c语言卒过河题目,洛谷过河卒编程题解

这题的做题思路就是要找准方向!

我相信各位,肯定尝试过dfs遍历统计,是不是最终以失败告终?

所以,这题的关键点是——在表格中,到达一个点的路径为这个点上面点的路径和这个点左边点的路径之和!

即下图所述意思:(本人画图不太行,理解一下)

| 5f749059187d35b3f20de366abecedf5.png

理解完这里就简单了,直接看代码把!

下面为本人源代码:

#include

using namespace std;

const int chessmax = 21;

//创建棋盘 重点!为啥用long long

//int 4 个字节 -2147483648 到 2147483647

//最后结果会溢出,而long long 足够大

long long chess[chessmax][chessmax];

void horse(int c,int d);

//初始化棋盘,到一个点的路径就是上一个点和左边点路径之和

void init()

{

chess[0][0] = 0;

chess[0][1] = 1;

chess[1][0] = 1;

for (int i = 0; i < chessmax; i++)

{

for (int j = 0; j < chessmax; j++)

{

if (i - 1 >= 0 && j - 1 >= 0)

chess[i][j] = chess[i - 1][j] + chess[i][j - 1];

else if (i - 1 > 0 && j - 1 < 0)

chess[i][j] = chess[i - 1][j];

else if (i - 1 < 0 && j - 1 > 0)

chess[i][j] = chess[i][j - 1];

}

}

}

//初始化马的位置 找出所有的绊脚点

void horse(int c,int d)

{

chess[c][d] = 0;

if (c >= 2 && d >= 1)chess[c - 2][d - 1] = 0;

if (c >= 1 && d >= 2)chess[c - 1][d - 2] = 0;

if (c >= 1)chess[c - 1][d + 2] = 0;

if (c >= 2)chess[c - 2][d + 1] = 0;

if (d >= 2)chess[c + 1][d - 2] = 0;

if (d >= 1)chess[c + 2][d - 1] = 0;

chess[c + 1][d + 2] = 0;

chess[c + 2][d + 1] = 0;

}

int main()

{

int a, b,c,d;

cin >> a >> b>>c>>d;

//初始化棋盘

init();

horse(c,d);

for (int i = 0; i < chessmax; i++)

{

for (int j = 0; j < chessmax; j++)

{//这是点睛之笔,把棋盘中的值刷新,得到最后结果的棋盘

if (chess[i][j] == 0)

chess[i][j] = 0;

else if (i - 1 >= 0 && j - 1 >= 0)

chess[i][j] = chess[i - 1][j] + chess[i][j - 1];

else if (i - 1 > 0 && j - 1 < 0)

chess[i][j] = chess[i - 1][j];

else if (i - 1 < 0 && j - 1 > 0)

chess[i][j] = chess[i][j - 1];

}

}

//最后输出结果就行了

cout << chess[a][b];

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值