One recursive approach to solve HDU 1016, list all permutations, solve N-queens puzzle.

Source: Internet
Author: User

One recursive approach to solve HDU 1016, list all permutations, solve N-queens puzzle.
Reference:the Video of Stanford cs106b Lecture by Julie Zelenski Https://www.youtube.com/watch?v=NdF1QDTRkck

HDU 1016

#include <cstdio>#include <cstring>#include <string>#include <algorithm>const INT Maxn= -; bool IsPrime (int k) {Static std::stringprime={3,5,7, One, -, -, +, at, in, to,Panax Notoginseng};returnPrime.find (k)!=STD::string:: NPOs;} void Printresult (std::stringSTR) {static char strbuf[2*maxn+5], *p; P=strbuf; for(Auto V:str) {p+=sprintf (P,"%d", (int) v); }    *--p=0;Puts (STRBUF);} void Recsolveprimering (std::stringSOFAR, std::string Rest) {if(Rest. Size () = =1) {if(IsPrime (Rest[0]+sofar. Back()) && IsPrime (Rest[0]+sofar.Front())) Printresult (sofar+Rest);return; } for(int i=0;i<Rest. Size (); ++i) {int x=Rest[I]+sofar. Back();if(IsPrime (Rest[I]+sofar. Back()) {recsolveprimering (sofar+Rest[I],Rest. substr (0, i) +Rest. substr (i+1)); }}}void solveprimering (int n) {static std::string Rest{‘\002‘};if(Rest. Back() <=n) for(int i=Rest. Back()+1; i<=n;++i)Rest. push_back (i);Else Rest. Resize (n1); Recsolveprimering ("\001",Rest);} int main () {#ifndef Online_judgeFreopen ("In.txt","R", stdin);#endifint n,k=0; while(SCANF ("%d", &n) = =1) {if(n>0&& N&LT;=MAXN && (n&1)==0) {printf ("Case%d:\n", ++k);            Solveprimering (n);        Putchar (' \ n '); }    }return 0;}

Permutation, from the video of Stanford cs106b lecture by Julie Zelenski

void Recpermute (stringSofar,string Rest) {if(Rest=="") {cout << sofar << Endl; }Else{ for(int i=Rest.length()-1; i>=0;I.) {            stringnext=sofar+Rest[i];stringremaining=Rest. substr (0, i) +Rest. substr (i+1);        Recpermute (next,remaining); }}}void Listpermutations (strings) {Recpermute ("", s);}

8-queens, can be extended to n-queens, limitation, n<=255, (Howevev 255 is a astronomical number for n-queens)
http://blog.csdn.net/qeatzy/article/details/46811451 contains my C + + code of Leetcode N-queens/n-queens II in this appr Oach

voidPrintqueenboard (stringSTR) {Static Charline[Ten]="........";Putchar(' ['); for(intI=0, tmp;i<8; ++i) {tmp=str[i]-' 0 '; line[tmp]=' Q ';printf("\"%s\ "", line); line[tmp]='. ';if(i==7)Putchar("],\n");Else puts(",");}voidRecsolvequeen (stringSofar,stringRest) {if(rest=="") {Printqueenboard (SOFAR); }Else{intFlag,len; for(intI=0; I<rest.length (); ++i) {flag=1; Len=sofar.length (); for(intj=0; j<len;++j) {if(Rest[i]-sofar[j]==len+i-j | | rest[i]-sofar[j]==j-i-len) {flag==0; Break; }            }if(flag) {Recsolvequeen (Sofar+rest[i],rest.substr (0, i) +rest.substr (i+1)); }        }    }}voidEightqueen () {strings="01234567";//or String s{' \001 ', ' \002 ',...};Recsolvequeen ("", s);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Ps. If in any-to-improment can be achieved, better performance or whatever, it'll be well-appreciated-let me know, thank s in advance.

One recursive approach to solve HDU 1016, list all permutations, solve n-queens puzzle.

Related Article

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.