Objective
Recent learning process, do not know what kind of thinking divergent, and suddenly met the Queen problem, and then the same cliché, the mind pondering, although the idea of everyone is easy to understand, even the simplest savage backtracking method, said simple, but if you have to code to achieve? Can I write out the code of OK at once? I questioned this, so I wrote the code, found that writing this kind of algorithm problem, the most important is the boundary condition judgment. Here, this article is purely practiced hand, do not consider algorithmic efficiency, just to achieve, in order to practice the most violent savage backtracking, plainly, is how simple rough play for and while these things!
Realize
I am lazy, so lazy to do two-dimensional array to store the queen coordinates, in fact, with a two-dimensional array to store, and then according to the column to traverse and match, it is really more efficient, but, I am just practicing backtracking here, so just use a simple array to store the number of n*n, such as 8 queen problem, I'll use the array to store the {Queens,,,,,,,,,, 64}, and then use the array to store the safe queen.
Code
<summary>///Queen algorithm Non-recursive implementation, the main idea is as follows:///Initialize the Queen alternative list is empty (safe), the pieces of all coordinates to try to put in///If the security is met, then put (if the number is reached to retain the output), if not safe skip continue search// /If the search end is still not found, then the loop goes back up to the next continuation of the last solution//</summary>public class queen{//matrix dimension int size; Numbered from 1, storing all integers int[] array; int count; Public Queen (): This (4) {} public Queen (int s) {this.size = s; Initlist (); } private void Initlist () {count = size * SIZE; Array = new Int[count]; for (int i = 0; i < count; i++) {Array[i] = i + 1; }} public void Printqueenresult () {list<int> queens = new list<int> (); Record Probe Index int tryindex = 0; Record the last time Queen int lastqueen = 0; Record Queens Total int queencount = 0; Record all queen solution number int solutions = 0; while (true) {//probe element int tryitem = Array[tryindex]; If the last element is detected and the Queen has gone back to empty//indicates that backtracking is all done, return if (Tryitem = = Array[count-1] && Queencount = = 0) break; Find the Safe Queen if (Issafe (Queens, Tryitem)) {Queens. ADD (Tryitem); Empress Number plus 1 queencount++; Record the last placed Queen Lastqueen = Tryitem; If the Queen gets enough, the output if (Queencount = = size) {Console.WriteLine ("Find Queen permutation: {0}", String. Join (",", Queens)); solutions++; }} else {if (Tryindex < count-1) { If it is not safe, search is not finished, continue to search tryindex++; } else {//if unsafe, but the search is over, then backtrack while (Tryindex >= Coun t-1) {Queens. Remove (Lastqueen); queencount--; TrYindex = Lastqueen; If the backtracking is complete, jump out if (Queencount = = 0) break; Lastqueen = queens[queencount-1]; }}}} Console.WriteLine ("Total number of solutions: {0}", solutions); } private bool Issafe (list<int> list, int target) {if (list = = NULL | | list. Count = = 0) return true; Return list. All (d =!) Isdangerous (d, Target)); } private bool Isdangerous (int a, int b) {int rowa = (A-1)/size + 1; int rowb = (b-1)/size + 1; int ColA = (A-1)% size + 1; int ColB = (b-1)% size + 1; if (a = = B) return true; if (Rowa = = ROWB) return true; if (ColA = = ColB) return true; if (Math.Abs (rowa-rowb) = = Math.Abs (Cola-colb)) return true; return false; } private int Pop (list<int> List) {int last = List[list. Count-1]; List. Remove (last); return last; } private int Peek (list<int> List) {return list[list. COUNT-1]; }}
Test
Eight Queens test code: Queen q = new Queen (8); Q.printqueenresult ();
Conclusion
The brain does not practice will rust, or should often do these most basic things, not accustomed to become a habit, slowly, not early, first sleep
A brief review of the--n Queen's Problem in algorithm review