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. javascript中利用柯里化函数实现bind方法

    柯理化函数思想:一个js预先处理的思想:利用函数执行可以形成一个不销毁的作用域的原理,把需要预先处理的内容都储存在这个不销毁的作用域中,并且返回一个小函数,以后我们执行的都是小函数,在小函数中把之前预 ...

  2. Javascript 字符串常用操作方法

    1.字符串转换 /* 你可以将一个数字,布尔值,或一个字符串对象转换为字符串 */ var num= 18; var str1 = num.toString(); //'18' var str2 = ...

  3. CSS关于子元素设置了float属性后父元素高度为0的解释和解决方法

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. windows无法访问指定设备路径或文件。您可能没有合适的权限访问

    试试退出杀毒软件试试,被杀毒软件阻拦了.

  5. iOS tabBar双击事件

    思路: 在tabBarController的代理方法 shouldSelectViewController 中, 通过判断tabBar选中的控制器是否是当前控制器 并 比对两次点击的时间间隔 来判断是 ...

  6. java归并排序,单线程vs多线程

    一.什么是归并排序 归并排序又称合并排序,它是成功应用分治技术的一个完美例子.对于一个需要排序的数组A[0..n-1],归并排序把它一分为二:A[0..n/2-1]和A[n/2..n-1],并对每个子 ...

  7. cookie记录用户名

    在说如何用cookie记录用户名之前,我们先来说说cookie的工作原理: cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据 ; 1. ...

  8. webpack ,gulp/grunt的介绍

    http://www.jianshu.com/p/42e11515c10f# bfc的概念block formatting context http://www.cnblogs.com/dojo-lz ...

  9. 推荐10款免费的在线UI测试工具

    发布网站之前至关重要的一步是网站测试.网站测试要求我们全面地运行网站并通过所有基本测试,如响应式设计测试.安全测试.易用性测试.跨浏览器兼容性.网站速度测试等. 网站测试对SEO.搜索引擎排名.转换率 ...

  10. 【APP自动化测试】Monkey的测试原理和方法

    参考资料:http://blog.csdn.net/io_field/article/details/52189972 一.Monkey测试原理:Monkey是Android中的一个命令行工具,可以运 ...