N皇后问题

在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。
你的任务是,对于给定的N,求出有多少种合法的放置方法。

Input
共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。
Output
共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。
Sample Input
1
8
5
0
Sample Output
1
92
10

这道题我老是Time Limit,有空还要优化一下,有大佬请帮我看下

////求N*N的格子摆放皇后的摆法(求有几种摆法而不是可以放多少个皇后)(递归) 
//#include<iostream>
//#include<algorithm>
//#include<string>
//#include<string.h>
//#include<cstdio>
//#include<queue>
//#include<stack> 
//#include<set> 
//using namespace std;
//int n,cnt;
//int vis[3][50];//vis[3]第一个中括号来区分不同访问类型三种类型,0(vis[0][])表示判断 同一列 的格子是否被访问过
////1(vis[1][])表示判断 右斜对角 的格子是否被访问过,2(vis[2][])表示判断 左斜对角 的格子是否被访问过
////#include<bits/stdc++.h>//万能头文件
//void dfs(int row){//把queen所在的行传入dfs 
//    if(row == n + 1 ){//递归出口条件、边界(如果超过第n行了) 
//        cnt++;//递归终止回溯一次,记录一次个数 
//        return ; 
//    }
//    for(int i = 1; i <= n; i++){
//        if(vis[0][i] == 0 && vis[1][n + row - i] == 0 && vis[2][i + row] == 0){
//            vis[0][i] = vis[1][n + row - i] = vis[2][i + row] = 1;
//            dfs(row + 1);
//            vis[0][i] = vis[1][n + row - i] = vis[2][i + row] = 0;//在深搜遍历的时候每一个格子的同行同列、左右对角都被访问标记了
//            //但是回溯的时候只是找到了一个合适的格子,(除该当前格子外)其他格子要重新恢复为未访问 
//        }
//    }
//}
//int main(){
//    memset(vis,0,sizeof(vis));
//    while(cin >> n,n != 0){
////    int cnt = 0;//记录皇后放置的数量,应定义为全局变量  
////    int n;//因为深搜里面也用到该变量,应定义为全局变量
//        cnt = 0; 
//        dfs(1); 
//        cout << cnt << endl;
//    }
//    return 0;
//}
// 

另一种方法,引入check函数,但本质还是递归

////求N*N的格子摆放皇后的摆法(求有几种摆法而不是可以放多少个皇后)(递归) 
//#include<iostream>
//#include<algorithm>
//#include<string>
//#include<string.h>
//#include<cstdio>
//#include<queue>
//#include<stack> 
//#include<set> 
//using namespace std;
//int n,cnt;
//int a[10];//a[i]表示第i行上的queen放于a[i]列上,如a[3] = 7表示第3行第7列的格子有queen 
////#include<bits/stdc++.h>//万能头文件 
////字符翻转 
//bool check(int x, int y){//传入行值和列值(相当于a[i]),看是否和queen所在的值相对应或同行同列斜对角 
//    for(int i = 1; i <= x; i++){//只用看在某行格子放queen之后,其前x行所对应的格子满足不满足条件,一个一个格子枚举对比 
//        if(a[i] == y){//check同列是否满足,应为是按行放,每行只放一个,同行无需验证 
//            return false;//判断a[i]的值是否等于1,2,3,4,5,6等dfs里面for(的i值),排除同列的
//            //不满足这个if及下面两个if就算找到了,结束check进行赋值操作a[row] = i 
//        }
//        if(i + a[i] == x + y){//check / 对角线是否满足 
//            return false;
//        }
//        if(i - a[i] == x - y){//check \ 对角线是否满足 
//            return false;
//        }    
//    }
//    return true;//注意要放在for循环外面,for全部执行之后没问题再返回true.都没有问题,返回queen可以放在这个格子 
//} 
//void dfs(int row){//把queen所在的行传入dfs 
//    if(row == n + 1 ){//递归出口条件、边界(如果超过第n行了) 
//        cnt++;//递归终止回溯一次,记录一次个数 
//        return ; 
//    }
//    for(int i = 1; i <= n; i++){
//        if(check(row,i)){//check一下第row行的皇后能不能放在第row行第i列上
//            a[row] = i; //可以放就给第row行的第i个(列)格子放上 
//            dfs(row + 1);
//            a[row] = 0;//进行完每次回溯把相应的第row行复原置零,重新开始,即没次dfs完后row行的皇后位置回到第一列开始往后找 
//        }      
//    }
//}
//int main(){
//    while(cin >> n,n != 0){
////    int cnt = 0;//记录皇后放置的数量,应定义为全局变量  
////    int n;//因为深搜里面也用到该变量,应定义为全局变量
//        cnt = 0; 
//        dfs(1); 
//        cout << cnt << endl;
//    }
//    return 0;
//}
// 

Prime Ring Problem

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.
image.png

Input
n (0 < n < 20).
Output
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.
Sample Input
6
8
Sample Output
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

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstdio>
#include<queue>
#include<stack> 
#include<set> 
using namespace std;
/*
素数环指的是将从1到n这n个整数围成一个圆环,若其中任意2个相邻的数字相加,结果均为素数
*/ 
int n,a[30],vis[30];//n是数字的个数,a[i]是需遍历的数组,vis[]是标记数组是否被访问过 
bool Prime(int yy)//判断素数
{
    for(int i=2; i<=sqrt(yy); i++)
    {
        if(yy%i==0)
        {
            return false;
        }
    }
    return true;
}
void dfs(int t){//此处的t只是传参,跟main函数里面的t没有关系 
    if(t == n && Prime(a[t - 1] + a[0])){//t是从0开始,当t=n时说明a[0,...t - 1]都填入了合规的数 
        for(int i = 0; i < t - 1; i++){//num从0开始 
            printf("%d ",a[i]);
        }
        printf("%d\n",a[t - 1]);//a[num - 1]就是传入的num下标对应的值 
    }else{//尝试可以填入a[i]的每一种可能性(穷举) 
        for(int i = 2; i <= n; i++){//i代表所有的可能性 
            if(vis[i] == 0 && Prime(i + a[t - 1])){//i没有使用过,还没连接在环上并且i和他前一位的和是素数 
                //此处i = 2是因为,dfs(1)从1开始并且 1 = a[1-1],所以从2 开始枚举(看2与1之和是否素数以及)
                vis[i] = 1;//用过的数标记1
                a[t] = i; //满足条件给所求数组的第一个位置a[1]赋值
                dfs(t + 1);//在a[1] = i基础上 对i之后的每个数继续递归判断
                vis[i] = 0;//进行完每次经过n此dfs后,该回溯的时候把相应的i复原置零,方便下一个传入的t继续枚举 
            }
        }
    } 
} 
int main(){
    int t = 1;//定义一个用来记录case后序号的变化 
    a[0] = 1;
    while(cin >> n){
        memset(vis,0,sizeof(vis));//标记数组vis初始化为0
        printf("Case %d:\n",t++);//输出一下头部格式,case后面的数字自增 
        dfs(1);//传入1开始进行深搜 
        printf("\n");
    }
}


沐小轲
9 声望0 粉丝

C++初学者