九度OJ-1459:Prime ring problem

   本题从理论上可以转化为对状态的搜索,用枚举法暴力求解。但是其状态参量有N个,若转化为状态搜索则其判断状态检索过与否的mark数组将有N维,从实际上来说难以实现,故应另辟蹊径。

  这是一道典型的深度优先搜索(遍历解答树)!!

Debug记录:

①第一次遇到真的因为cout而TLE的情况。由于此题当n=16时会出现爆炸性的输出,故cout比printf多出来的时间消耗已经不容忽视。将cout改为printf后AC。

②OJ上不知道是哪个版本的编译器,总之<iostream>不带有c的scanf、printf等诸多基础输入输出函数,需要#include <cstdio>

③以后使用递归函数时候记得尽量统一出口,这样显得逻辑清晰。例如写此题时,先前没有统一DFS的返回出口,导致只在 “if (d.size()==n){ ”处(即满队情况的分支处)写了出口,而没在非满队的分支处写出口。本来非满队的分支处是不需要写出口的,但是由于我把退栈操作(退回上层前的回溯)统一写在了本层return之前,导致当此次DFS在非满队分支执行并且执行完毕返回上层时,没有了退栈回溯操作,导致出错。

收获如下:

①掌握了双端队列#include <deque>的用法(写法基本与<vector>一致),具体见STL容器专题。

②掌握了此类题目的做法:转化为对解答树的先序遍历(深度优先搜索),而非使用循环。因为若使用循环共需嵌套N层,从实际上来说实在难以实现。

③掌握了回溯法。回溯法(结束此层DFS时退栈)在此类对解空间的先序遍历(深度优先搜索)中经常使用,更是用来求解树的路径序列问题的必要方法。这个回溯操作放在统一 返回出口处写(自己的写法)是逻辑清晰的,不要像《指南》里头那样写,东一个西一个很不清晰很容易出bug。


题目描述:

A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.


输入:

n (1 < n < 17).

输出:

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.

样例输入:
6
8
样例输出:
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
提示:

用printf打印输出。



#include <iostream> 
#include <cstdio>
#include <cmath>
#include <deque>//由于回溯操作要用到删除队尾元素,故使用双端队列 
#define MAXSIZE 17
using namespace std;
deque<int> d;
bool prime[MAXSIZE*2+1];
bool mark[MAXSIZE+1];
int n;
void DFS(){
	if (d.size()==n){//当凑齐prime ring时 
		if (prime[d.back()+d.front()]==true){//若首尾满足条件,即已完成一个prime ring =>print 	
			deque<int> e=d;//声明临时队列e 
			bool firstCase=true;
			while (!e.empty()){
				if (firstCase==true)
					firstCase=false;
				else
					printf(" ");
				printf("%d",e.front());
				e.pop_front();
			}
			printf("\n");
		}
		else{//不做任何事 
		}
	}
	else{
		for (int i=2;i<=n;i++){//add a number 
			if (mark[i]==false&&prime[i+d.back()]==true){//若i还未放入q且满足i与前项和为素数 
				d.push_back(i);
				mark[i]=true;
				DFS();//继续添加 
				//下层已自动回溯 
			}
		}
	}
	//统一出口: 
	//无论成功或者失败,都在此返回上层,并且返回前都要回溯 
	mark[d.back()]=false;
	d.pop_back();
	return;
}
int main(){
	//initiate
	int primeBound=2;
	prime[0]=prime[1]=false;
	prime[2]=true;
	bool isPrime=true;
	int time=1;
	//preprocess
	for (int i=3;i<MAXSIZE*2+1;i++){//判断i是否为素数 
		isPrime=true;//initiate isPrime
		for (int j=2;j<=primeBound&&j<sqrt(i)+1;j++){//遍历素数组来尝试整除 
			if (prime[j]==true){
				if (i%j==0){//能整除
					isPrime=false;
					break;
				} 
			}
		}
		prime[i]=isPrime;
		if (isPrime){
			primeBound=i;
		}
	}
	//body
	while (cin>>n){
		//initiate 
		while (!d.empty())
			d.pop_front();
		for (int i=0;i<=n;i++){
			mark[i]=false;
		}
		d.push_back(1);
		mark[1]=true;
		//search
		cout<<"Case "<<time<<":"<<endl;
		DFS();
		printf("\n");
		time++; 
	}
	return true;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值