这题的做题思路就是要找准方向!
我相信各位,肯定尝试过dfs遍历统计,是不是最终以失败告终?
所以,这题的关键点是——在表格中,到达一个点的路径为这个点上面点的路径和这个点左边点的路径之和!
即下图所述意思:(本人画图不太行,理解一下)
|
理解完这里就简单了,直接看代码把!
下面为本人源代码:
#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;
}