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, 795MS

#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;
}

// improved version for hdu 1016, 483MS,

// encapsulated to a Solution class, function isprime more speedy,

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm> class SolutionPrimeRing {
static const std::string primetable;
static std::string prime;
static inline bool isPrime(int k) {
return (k&1) && prime.find(k)!=std::string::npos;
} static void printResult(const 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);
} static void recSolvePrimeRing(std::string soFar, std::string rest) {
static int tmp;
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) {
if(isPrime(rest[i]+soFar.back())) {
recSolvePrimeRing(soFar+rest[i],rest.substr(0,i)+rest.substr(i+1));
}
}
} public:
static const int MAXN=20; static void solve(int n) {
if(n>MAXN || n<2 || (n&1)) { return; }
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); prime.clear();
n<<=1;
for(int i=0;primetable[i]<n;++i) {
prime.push_back(primetable[i]);
}
recSolvePrimeRing("\001",rest);
}
};
const std::string SolutionPrimeRing::primetable={3,5,7,11,13,17,19,23,29,31,37,41};
std::string SolutionPrimeRing::prime; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n,k=0;
while(scanf("%d",&n)==1) {
printf("Case %d:\n",++k);
SolutionPrimeRing::solve(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);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. 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 for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏的更多相关文章

  1. hdu 1052 (greedy algorithm) 分类: hdoj 2015-06-18 16:49 35人阅读 评论(0) 收藏

    thanks to http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=19638&m ...

  2. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏

    a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...

  5. hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏

    use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...

  6. Improving the GPA 分类: 贪心 HDU 比赛 2015-08-08 16:12 11人阅读 评论(0) 收藏

    Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  7. Task schedule 分类: 比赛 HDU 查找 2015-08-08 16:00 2人阅读 评论(0) 收藏

    Task schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  9. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 如何在自己的窗体(控件)中显示XAF的视图

    Form form = new Form(); DevExpress.ExpressApp.View listView = Application.CreateListView(Application ...

  2. sublime text 3插件

    Package Control Messages Emmet emmet插件 Thank you for installing Emmet -- a toolkit that can greatly ...

  3. MarkDown常用语法记录

    目录 1. 斜体和粗体 2. 分级标题 3. 超链接 3.1 行内式(推荐) 3.2 行外式 3.3 自动链接 4. 锚点 5. 列表 5.1无序列表 5.2有序列表 6. 引用 7. 插入图像 8. ...

  4. css3——新盒子定义box-sizing

    css3对盒子有了新定义,以前的 盒子实际宽(高) = padding + width(height) + ( border * 2); 使用了box-sizing之后盒子实际宽(高) 就等于 wid ...

  5. Redis教程(三) list类型

     一.概述: redis的list类型其实就是一个每个子元素都是string类型的双向链表.所以[lr]push和[lr]pop命令的算法时间复杂度都是O(1) 另外list会记录链表的长度.所以ll ...

  6. Session的使用过程中应注意的一个小问题

    在学习AllEmpty大神的从零开始编写自己的C#框架系列文章中,发现的问题:在验证码的缓存Session["vcode"]的赋值时,发现Session["vcode&q ...

  7. Opentaps安装小记

    这周了解了个MES项目需求,于是乎找了些开源项目了解下,有php+mysql写的weberp,配置起来相当方便,下一版webERP_v4.13,往F:\Apache\htdocs\一放,启动F:\Ap ...

  8. WORD 粘贴代码 不检查语法

  9. [ubunut]打造Ubuntu下Java开发环境 (转)

    http://www.cnblogs.com/wufengtinghai/p/4542366.html 遇到困难: A Java Runtime Environment (JRE) or Java D ...

  10. iOS多线程

    关于iOS多线程 概述 这篇文章中,我不会说多线程是什么.线程和进程的区别.多线程有什么用,当然我也不会说什么是串行.什么是并行等问题,这些我们应该都知道的. 在 iOS 中其实目前有 4 套多线程方 ...