Full arrangement/recursion/eight queens

Source: Internet
Author: User
1st elements and 2nd, 3, 4, 5 .. len-1 elements are exchanged, respectively.
 
VoidPermutation (Char* STR,IntBegin,IntLen ){If(LEN = 1) {printf ("% S \ n", STR); count ++;Return;}CharTMP = STR [begin];For(IntI = 0; I <Len; I ++ ){CharTmp2 = STR [begin + I]; STR [begin] = tmp2; STR [begin + I] = TMP; permutation (STR, begin + 1, len-1 ); STR [begin] = TMP; STR [begin + I] = tmp2 ;}}
 
 
 
 
 
CharSTR [6]; strcpy (STR ,"12345"); Permutation (STR, 0, 5 );

 

All combinations: http://zhedahht.blog.163.com/blog/static/2541117420114172812217/
 
 

 Void Combination ( Char * String , Int Number, vector < Char > & Result ); Void Combination ( Char * String ){ If ( String = NULL) Return ; Int Length = strlen ( String ); Vector < Char > Result; For ( Int I = 1; I <= length; ++ I) {combination ( String , I, result );}} Void Combination ( Char * String , Int Number, vector <Char > & Result ){ If (Number = 0) {vector < Char >:: Iterator iter = result. Begin (); For (; ITER <result. End (); ++ ITER) printf (" % C ", * ITER); printf (" \ N "); Return ;} If (* String = '\ 0 ') Return ; // One character has two options
Result. push_back (*String); Combination (String+ 1, number-1, result); Result. pop_back ();
 
Combination (String+ 1, number, result );}

----------------------------------------------------------------------------

Eight queens Problem: Col, row + Col, row-Col three mark Arrays

 // 8queen. cpp: defines the entry point for the console application.  // # Include" Stdafx. h "# Include <iostream> Using   Namespace STD; Const  Int Size = 8; Const   Int Offset = size-1; Const   Int Flat_size = 3 * size-2; // 2 * (size-1) + (size-1) + 1  Int Sov [size]; Bool Hasa [flat_size] = { False }; Bool Hasb [flat_size] = { False }; Bool Hasc [size] = { False };Bool Couldstand ( Int R, Int C ){ Return ! Hasc [c] &! Hasa [R + C + offset] &! Hasb [R-C + offset];} Int M_count = 0; Void T ( Int R ){ For ( Int C = 0; C <size; C ++ ){ If (Couldstand (R, c) {Sov [R] = C; hasa [R + C + offset] = True ; Hasb [R-C + offset] = True ; Hasc [c] = True ; If (R = offset ){ For ( Int I = 0; I <size; I ++) printf (" % D ", Sov [I] + 1); printf (" \ N "); M_count ++ ;} Else T (R + 1); hasa [R + C + offset] = False ; Hasb [R-C + offset] = False ; Hasc [c] = False ;}}} Int Main ( Int Argc,Char * Argv []) {memset (hasc, False , Sizeof (Hasc); t (0); printf (" Hello world % d! \ N ", M_count ); Return 0 ;}

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.