The 8 queen problem of backtracking algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.