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. KEEPALIVED

    keepalived 一.原理: 功能:能够自动实现将用户访问的ip转移的方法,故障重启,故障,恢复切换,故障报警 模型:Master/Backup 使用场景:节点少,没有共享存储等等.他只能有一个主 ...

  2. 参考__CSS参考

    库 CsshakeAnimate.css

  3. MFC编程入门之十九(对话框:颜色对话框)

    在上一节中为大家讲解了字体对话框的使用方法,熟悉了字体对话框,本节继续讲另一种通用对话框--颜色对话框. 颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用是用来选择颜色.MF ...

  4. 【Java】多线程_学习笔记

    多线程 1.进程 进程:当一个程序进入内存运行时,它就成为了进程.进程具有独立性.动态性.并发性. A.独立性:进程是系统中独立存在的实体,它可以拥有自己独立的资源,每一个进程都拥有自己私有的地址空间 ...

  5. typeof,GetType

    typeof: 是运算符,获得某一类型的 System.Type 对象. Int32 t = new Int32(); Type t = typeof(int); GetType: 是方法,获取当前实 ...

  6. eclipse rcp 打包出适合不同操作系统和操作位数.

    http://blog.csdn.net/luoww1/article/details/8677999 http://blog.csdn.net/soszou/article/details/8053 ...

  7. [Java] SoapUI使用Java获取各时间日期方法

    import java.util.*; import java.text.SimpleDateFormat; // current date String dateNew = today() // t ...

  8. 行内js函数调用

    <ul> <li onclick=abc(this);><a href="javascript:void(0);">12234588</a ...

  9. centos5.11 repo 安装mysql5.7

    http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html mysql yum repo 安装说明 http://d ...

  10. message from server: "Host 'XXX' is not allowed to connect to this MySQL server

    Access denied for user 'root'@'XXXXX' (using password: YES) mysql命令不正确造成: grant all privileges on *. ...