当前位置:网站首页>Difference between backtracking and recursion
Difference between backtracking and recursion
2022-07-06 06:26:00 【jq_ ninety-eight】
Look at the data structure recently , It is found that many recursion and backtracking problems are used , I really don't know the difference between the two , I checked some information recently , Let's summarize .
In order to describe a certain state of the problem , The previous state of this state must be used , And describe the previous state , You must use the previous state of the previous state …… This method of defining yourself by yourself , Called recursive definition . In the form of f(n) = n*f(n-1), if n=0,f(n)=1.
Starting from one possibility of the problem , Search for all the possibilities that can be achieved from this situation , When this road comes to ” At the end of “ When , Go back to the starting point , From another possibility , Continue to search . This continuous ” to flash back “ The way to find the solution , Referred to as ” backtracking “.
Recursion is an algorithm structure , Recursion will appear in the subroutine, calling itself or indirectly calling itself . The most direct recursive application is to calculate the factorial of continuous numbers , The law of calculation :n!=(n-1)!*n.
Observe the law of factorial calculation , The result of the former number formation can be directly applied to the calculation of the latter number formation .
int fac(int n)
if(n==1)
return n;
else
return n*fac(n-1);
Backtracking is an algorithmic idea , It can be implemented recursively . Generally speaking, backtracking is a kind of Temptation , Similar to exhaustive , But backtracking has “ prune ” function , For example, the summation problem . Given 7 A digital ,1 2 3 4 5 6 7 Sum equals 7 The combination of , Search from small to large , choice 1+2+3+4 =10>7, Already exceeded 7, After that 5 6 7 There's no need to continue , This is an optimization of the search process . If there is anything unclear, you can have a look 8 queens problem .
Link to the original text :https://blog.csdn.net/u014772862/article/details/51789015
边栏推荐
- 模拟卷Leetcode【普通】1218. 最长定差子序列
- Postman核心功能解析-参数化和测试报告
- leetcode 24. Exchange the nodes in the linked list in pairs
- 生物医学英文合同翻译,关于词汇翻译的特点
- Simulation volume leetcode [general] 1405 Longest happy string
- 关于新冠疫情,常用的英文单词、语句有哪些?
- Cannot create poolableconnectionfactory (could not create connection to database server. error
- Simulation volume leetcode [general] 1091 The shortest path in binary matrix
- 【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境
- Postman core function analysis - parameterization and test report
猜你喜欢
Black cat takes you to learn UFS protocol Chapter 4: detailed explanation of UFS protocol stack
B - The Suspects
[web security] nodejs prototype chain pollution analysis
Basic knowledge of MySQL
把el-tree选中的数组转换为数组对象
国产游戏国际化离不开专业的翻译公司
F - True Liars (种类并查集+DP)
Full link voltage measurement: building three models
关于新冠疫情,常用的英文单词、语句有哪些?
Win10 cannot operate (delete, cut) files
随机推荐
Is the test cycle compressed? Teach you 9 ways to deal with it
Isam2 operation process
MySQL is sorted alphabetically
黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
国产游戏国际化离不开专业的翻译公司
基于JEECG-BOOT制作“左树右表”交互页面
University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
Simulation volume leetcode [general] 1109 Flight reservation statistics
在uni-app中使用腾讯视频插件播放视频
通过修改style设置打印页样式
Postman core function analysis - parameterization and test report
模拟卷Leetcode【普通】1314. 矩阵区域和
Past and present lives of QR code and sorting out six test points
Simulation volume leetcode [general] 1143 Longest common subsequence
模拟卷Leetcode【普通】1109. 航班预订统计
自定义指定路由上的Gateway过滤器工厂
ESP32 ESP-IDF看门狗TWDT
Database - current read and snapshot read
An article was uncovered to test the truth of outsourcing companies
JWT-JSON WEB TOKEN