one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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) ...
- 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 ...
- 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 ...
- 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) ...
随机推荐
- 备份MySQL数据库
备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http:// ...
- Javascript中的集合
集合是由一组无序且唯一(即不能重复)的项组成 function Set() { var items={}; this.has=function(value){ //return value in it ...
- 实验一 认识DOS
#include<stdio.h> #include<string.h> void main() { char cmd[20][20]={"dir&quo ...
- spring-task
cronExpression的配置说明,具体使用以及参数请百度google 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小 ...
- JS巧计__轮播
横向轮播 function lxfScroll(main,titleli,alt,speed){ var lxfscroll = $(main); var ul = lxfscroll.find(&q ...
- Visual Studio最常用、最高效的快捷键
查了一些VS编程的快捷键,大家共同学习,共同进步! 1.强迫智能感知:Ctrl+J.智能感知是Visual Studio最大的亮点之一,选择Visual Studio恐怕不会没有这个原因. 2.强迫显 ...
- string 常量池的理解
1: String a="123"; String b="12"+"3"; String c="1"+"23& ...
- JavaWeb 自定义404页面
本来,Tomcat中自定义404页面不过是在web.xml文件中写4行代码的事情. 直接引用 Tomcat官方FAQ 怎样自定义404页面? 编辑web.xml <error-page> ...
- 《精通C#》十四章-.NET程序集入门
在书中,这一章节的开头说的是自定义命名空间和使用命名空间,在以我目前有限的经验来说,程序集就是一个类库经过编译之后,所生成的一个在引用命名空间,进而使用该文件中已经定义好的字段,属性以及方法的文件,以 ...
- Javascript学习笔记3 Javascript与BOM简介
什么是BOM BOM是browser object model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象 ...