usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceseqlistsort{/// <summary> ///8 Queens Algorithm/// <ather> /// <lihonglin> /// </ather> /// </summary> classEightqueen {Private Const intCOL =8;//Line Private Const intROW =8; Private Static int[,] map =New int[COL, ROW];//Chess Board Private Static intCount =0;//number of species placed in the Queen's position//Place Queen Public Static voidPutqueen (introw) { inti =0; if(row = = row)//End Condition{Display (); return; } //one column, one column . for(i =0; i < COL; ++i) {if(IsOK (row, i)) {map[row, I]=8; Putqueen (Row+1); //BacktrackingMap[row, I] =0; } } } //Placement Rules Public Static BOOLIsOK (intXinty) { for(inti =0; i < COL; i++) { for(intj =0; J < ROW; ++j) {//placing rules, the same row, the same column, the position of the hypotenuse and the reverse hypotenuse can not be placed if(i = = x | | j = = y | | (x + y = = i + j) | | (x-y = = i-j)) {if(8==Map[i,j]) { return false; } } } } return true; } Public Static voidDisplay () {Count++; Console.WriteLine ("type {0}", Count); for(inti =0; i < COL; i++) { for(intj =0; J < ROW; ++j) {Console.Write (" "+Map[i,j]); } Console.WriteLine (); } } }}
The 8 queen problem of backtracking algorithm