This article describes the C# method to avoid backtracking and is shared with you for your reference. The specific analysis is as follows:
First of all, the backtracking method is uncontrollable, and sometimes it will produce bad results beyond our expectations. The most common one is memory leakage. .
The backtracking method is easy to think of but not easy to think of. Often, what we think more easily is the backtracking method. However, backtracking has its disadvantages. The obvious disadvantage is that variables and references generated in scope cannot be released when the backtracking method call is not completed (for most editors, editors with optimization capabilities are excluded). If we use a lot of backtracking calls in a certain method and cannot release variables and references within the scope of the method in a timely manner in the method, it will eventually lead to insufficient memory and increased CPU computing load (the excess data can be transferred to virtual memory and hard disk in the memory mechanism, so I won’t talk about this). Using stack (team)-style loops can easily avoid backtracking, and stack (team)-style data can be easily thrown and removed after use. This is sometimes the case, a small change can bring a program back to life in a certain environment. (I used to do an algorithm for Sudoku, and later optimization and improvement were to avoid backtracking)
The sample code is as follows:
using System; using ; using ; using ; using ; namespace Avoid backtracking methods { class Program { static void Main(string[] args) { string path = ; List<string> fileList1 = new List<string>(); FunctionHuishuo(path, ref fileList1); List<string> fileList2 = new List<string>(); FunctionQueue(path, ref fileList2); List<string> fileList3 = new List<string>(); FunctionStack(path, ref fileList3); } /// <summary> /// Backtracking method /// </summary> /// <param name="path"></param> /// <param name="fileList"></param> private static void FunctionHuishuo(string path, ref List<string> fileList) { if (true) { string[] files = null; try { files = (path); } catch { } if (files != null && > 0) { (files); } } if (true) { string[] folders = null; try { folders = (path); } catch { } if (folders != null && > 0) { foreach (string folder in folders) { FunctionHuishuo(folder, ref fileList); } } } } /// <summary> /// Stack method /// </summary> /// <param name="path"></param> private static void FunctionStack(string path, ref List<string> fileList) { Stack<string> stack = new Stack<string>(); (path); while ( > 0) { string dir = (); string[] files = null; try { files = (dir); } catch { } if (files != null && > 0) { (files); } string[] folders = null; try { folders = (dir); } catch { } if (folders != null && > 0) { foreach (string folder in folders) { (folder); } } } } /// <summary> /// Queue method /// </summary> /// <param name="path"></param> private static void FunctionQueue(string path, ref List<string> fileList) { Queue<string> queue = new Queue<string>(); (path); while ( > 0) { string dir = (); string[] files = null; try { files = (dir); } catch { } if (files != null && > 0) { (files); } string[] folders = null; try { folders = (dir); } catch { } if (folders != null && > 0) { foreach (string folder in folders) { (folder); } } } } } }
Please carefully compare the writing methods of the three loop structures, and pay special attention to the use of if(true){...} . This method can produce very wonderful effects in some editing environments.
I believe that this article has certain reference value for everyone's learning C# programming.