码迷,mamicode.com
首页 > 移动开发 > 详细

one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle.

时间:2015-07-19 18:07:58      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:hdu   recursion   backtrack   c++   

one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle.
reference: the video of stanford cs106b lecture 10 by Julie Zelenski https://www.youtube.com/watch?v=NdF1QDTRkck

//hdu 1016

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

const int MAXN=20;

bool isPrime(int k) {
    static std::string prime={3,5,7,11,13,17,19,23,29,31,37};
    return prime.find(k)!=std::string::npos;
}

void printResult(std::string str) {
    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::string soFar, 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(n-1);
    recSolvePrimeRing("\001",rest);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n,k=0;
    while(scanf("%d",&n)==1) {
        if(n>0 && n<=MAXN && (n&1)==0) {
            printf("Case %d:\n",++k);
            solvePrimeRing(n);
            putchar(‘\n‘);
        }
    }

    return 0;
}

// Permutation, from the video of stanford cs106b lecture 10 by Julie Zelenski

void RecPermute(string soFar, string rest) {
    if(rest=="") {
        cout << soFar << endl;
    }
    else {
        for(int i=rest.length()-1;i>=0;--i) {
            string next=soFar+rest[i];
            string remaining=rest.substr(0,i)+rest.substr(i+1);
            RecPermute(next,remaining);
        }
    }
}
void ListPermutations(string s) {
    RecPermute("",s);
}

// 8-Queens, 可以推广到N-queens, limitation, N<=255,(howevev 255 is an 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 approach

void printQueenBoard(string str) {
static char line[10]="........";
putchar(‘[‘);
for(int i=0, tmp;i<8;++i) {
    tmp=str[i]-‘0‘;
    line[tmp]=‘Q‘;
    printf("\"%s\"",line);
    line[tmp]=‘.‘;
    if(i==7) putchar("],\n");
    else puts(",");
}

void RecSolveQueen(string soFar, string rest) {
    if(rest=="") {
        printQueenBoard(soFar);
    }
    else {
        int flag,len;
        for(int i=0;i<rest.length();++i) {
            flag=1;
            len=soFar.length();
            for(int j=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));
            }
        }
    }
}

void eightQueen() {
    string s="01234567";
        // or string s{‘\001‘,‘\002‘,...};
    RecSolveQueen("",s);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// ps. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle.

标签:hdu   recursion   backtrack   c++   

原文地址:http://blog.csdn.net/qeatzy/article/details/46955979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!