当前位置:网站首页>Routine solution - the problem of horse walking on the chessboard
Routine solution - the problem of horse walking on the chessboard
2022-06-10 19:52:00 【Starlight technician】
The problem of horse walking on the chessboard
subject : The chess board assumes a horizontal line 10 strip , A vertical bar 9 strip ; The lower left corner is the position origin (0,0); Suppose the horse is now at the origin , jump 10 Time just jumps to (7,7) How many jumping methods are there ;
- Recursive writing
- code
// Brute force recursive algorithm
// chess " Horse " from (0,0) Let's go and jump rest Step by step (aim_x,aim_y) How many measurements are there
// Look at it in reverse (aim_x,aim_y) To (0,0) How many jumps are there
//cur_x,cur_y Indicates chess " Horse " Now the difference from the target position
int get_N1(int cur_x, int cur_y, int rest)
{
// Chess cannot cross the line , Out of bounds means that this method is invalid
if (cur_x > 8 || cur_x < 0 || cur_y < 0 || cur_y > 9)
return 0;
// The steps are just finished
if (rest == 0)
{
// The target position is not reached
if (cur_x !=0 || cur_y != 0)
return 0;
// Get to the target position
return 1;
}
// There are still steps left
int p1 = get_N1(cur_x + 1, cur_y + 2, rest - 1);
int p2 = get_N1(cur_x + 1, cur_y - 2, rest - 1);
int p3 = get_N1(cur_x - 1, cur_y + 2, rest - 1);
int p4 = get_N1(cur_x - 1, cur_y - 2, rest - 1);
int p5 = get_N1(cur_x + 2, cur_y + 1, rest - 1);
int p6 = get_N1(cur_x + 2, cur_y - 1, rest - 1);
int p7 = get_N1(cur_x - 2, cur_y + 1, rest - 1);
int p8 = get_N1(cur_x - 2, cur_y - 1, rest - 1);
return p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
}
You can think of the recursive solution as an octree ; The node asked eight children for information
- Strict table structure solution
This is a three-dimensional dp Array , according to basecase You can get dp[0][0][0] = 1;
dp[i][j][0] stay i,j When not all are zero , by 1; Fill in part first dp; Then according to the location dependency , Specify the traversal order of the axes , And the traversal order of each axis
- code
// Strict table structure
int getValue(vector<vector<vector<int>>>& dp, int x, int y, int z)
{
if (x < 0 || x > 8 || y < 0 || y > 9 || z < 0)
{
return 0;
}
return dp[x][y][z];
}
int get_N3(int cur_x, int cur_y, int rest)
{
if (cur_x > 8 || cur_x < 0 || cur_y < 0 || cur_y > 9 || rest < 0)
return 0;
vector<vector<vector<int>>> dp(9, vector<vector<int>>(10, vector<int>(rest+1,0)));
dp[0][0][0] = 1;
for (int z = 1; z <= rest; z++)
{
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 10; y++)
{
dp[x][y][z] += getValue(dp, x - 1, y - 2, z - 1);
dp[x][y][z] += getValue(dp, x - 1, y + 2, z - 1);
dp[x][y][z] += getValue(dp, x + 1, y - 2, z - 1);
dp[x][y][z] += getValue(dp, x + 1, y + 2, z - 1);
dp[x][y][z] += getValue(dp, x - 2, y - 1, z - 1);
dp[x][y][z] += getValue(dp, x - 2, y + 1, z - 1);
dp[x][y][z] += getValue(dp, x + 2, y - 1, z - 1);
dp[x][y][z] += getValue(dp, x + 2, y + 1, z - 1);
}
}
}
return dp[cur_x][cur_y][rest];
}
边栏推荐
- Analyse du code source de Tencent libco CO CO - Process open source library
- 【C语言进阶】指针的进阶【中篇】
- 2022.05.24 (lc_674_longest continuous increasing sequence)
- 2022.05.29 (lc_6078_rearranges characters to form target string)
- An error row size too large (& gt; 8126) occurs when MySQL's MyISAM engine switches to InnoDB
- 改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
- Micronet practice: image classification using micronet
- 如何在VR全景作品中添加独立热点?
- Before we learn about high-performance computing, let's take a look at its history
- 轻松学Pytorch-全卷积神经网络实现表情识别
猜你喜欢

Design and implementation of SSM based traffic metering cloud system Rar (thesis + project source code)

领域驱动设计(六) - 架构设计浅谈

SAR图像聚焦质量评价插件

Easily learn pytoch full convolution neural network to realize expression recognition

软件测试月薪10K如何涨到30K,只有自动化测试能做到

DDD landing practice repeat record of theoretical training & Event storm

One article explains in detail the exploration and practice of eventmesh landing on Huawei cloud

Sliding window maximum value problem

VR全景作品中各式各样的嵌入功能是如何做到的?

How do big factories write data analysis reports?
随机推荐
mixin-- 混入
深入理解LightGBM
2022.05.25 (lc_718_longest repeating subarray)
面试中经常问到的几个问题,快来看看能答对几道吧
详细解读TPH-YOLOv5 | 让目标检测任务中的小目标无处遁形
[C language] have you mastered these classic questions? Learn these questions in one article
100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios
今年高考期间各考点秩序井然,未发生影响安全的敏感案事件
Source code analysis of Tencent libco collaborative process open source library (II) -- persimmon starts from the soft pinch, and the sample code officially begins to explore the source code
云图说|每个成功的业务系统都离不开APIG的保驾护航
金融行业的密钥及加密机制
When the college entrance examination is opened, VR panorama can see the test site in this way
轻松学Pytorch-全卷积神经网络实现表情识别
Tidb - quick start, cluster setup
Rmarkdown 轻松录入数学公式
叮咚抢菜-派送时段监听及推送工具
Developers changing the world - Yao Guang teenagers playing Tetris
Analyse du code source de Tencent libco CO CO - Process open source library
Sliding window maximum value problem
领域驱动设计(六) - 架构设计浅谈