当前位置:网站首页>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
边栏推荐
- Pat (Grade B) 2022 summer exam
- University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
- Apple has open source, but what about it?
- Black cat takes you to learn UFS Protocol Part 8: UFS initialization (boot operation)
- 自定义指定路由上的Gateway过滤器工厂
- MFC on the conversion and display of long string unsigned char and CString
- php使用redis实现分布式锁
- Remember the implementation of a relatively complex addition, deletion and modification function based on jeecg-boot
- 记一个基于JEECG-BOOT的比较复杂的增删改功能的实现
- Left matching principle of joint index
猜你喜欢

Esp32 esp-idf watchdog twdt

D - How Many Answers Are Wrong

ESP32 ESP-IDF看门狗TWDT

数据库隔离级别

The whole process realizes the single sign on function and the solution of "canceltoken" of undefined when the request is canceled

Full link voltage measurement: building three models

Defense (greed), FBI tree (binary tree)

Black cat takes you to learn EMMC Protocol Part 10: EMMC read and write operation details (read & write)

LeetCode 732. My schedule III

On weak network test of special test
随机推荐
「 WEB测试工程师 」岗位一面总结
MFC dynamically creates dialog boxes and changes the size and position of controls
D - How Many Answers Are Wrong
LeetCode 739. Daily temperature
LeetCode 1200. 最小绝对差
Apple has open source, but what about it?
Simulation volume leetcode [general] 1143 Longest common subsequence
MFC关于长字符串unsigned char与CString转换及显示问题
浅谈专项测试之弱网络测试
Past and present lives of QR code and sorting out six test points
B - The Suspects
LeetCode 1200. Minimum absolute difference
Black cat takes you to learn EMMC Protocol Part 10: EMMC read and write operation details (read & write)
[web security] nodejs prototype chain pollution analysis
oscp raven2靶机渗透过程
数据库隔离级别
mysql按照首字母排序
模拟卷Leetcode【普通】1249. 移除无效的括号
selenium源码通读·9 |DesiredCapabilities类分析
模拟卷Leetcode【普通】1143. 最长公共子序列