Eight Queens--python code

Source: Internet
Author: User

Application of iterative and recursive methods

ImportRandomdefPrettyprint (solution):#graphical processing of data    defLine (Pos,length=len (solution)):#processing of single-row data        return '.'* (POS) +'x'+'.'* (length-pos-1)     forPosinchSolution:#draw a graph of every eight lines        PrintLine (POS)defConflict (STATE,NEXTX):#detect if there is a conflictNexty =Len (state) forIinchRange (nexty):ifABS (STATE[I]-NEXTX)inch(0,nexty-i):#Compare by Distance            returnTruereturnFalse#def Queens (Num,state):#If Len (state) = = Num-1:#For POS in range (num):#If not conflict (State,pos):#yield PosdefQueens (num=8,state=()):     forPosinchrange (num):Print Stateif  notconflict (state,pos):ifLen (state) ==num-1:#last one                yield(POS,)#Iterative Methods            Else:                 forResultinchQueens (Num,state+ (POS)):#recursive top seven data#Print Result                    yield(POS,) +resultPrint 'Bahuanghou'PrintList (Queens (4))#For I in List (Queens (8)):#Prettyprint (i)#print ' Next '#prettyprint (Random.choice (List (Queens (8) ))

Eight Queens--python code

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.