N皇后问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29378    Accepted Submission(s): 12879

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

 
Input
共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。
 
Output
共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。
 
Sample Input
1
8
5
0
 
Sample Output
1
92
10
 
Author
cgf
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1016 1312 1181 1241 1010 
 
#include <iostream>
#include <cmath>
using namespace std;
int set[13]; //放置纵坐标
int n, sum;
int zl[13]; void dfs(int x){
if(x > n){
sum++;
return ;
}
for(int y = 1; y <= n; y++){ //枚举每一列
int i;
for(i = 1; i < x; i++){ //检测该列
if(set[i] == y)
break;
}
if(i < x){ //列有人
continue;
}
for(i = 1; i < x; i++){ //检测对角线,通过斜率绝对值为1来判断
if(x - i == abs(y - set[i]))
break;
}
if(i < x){
continue;
}
set[x] = y; //找到了合适的列,填进去
dfs(x + 1);
}
} int main(){
std::ios::sync_with_stdio(false);
for(n = 1; n <= 10; n++){
sum = 0;
dfs(1); //每次从第一行放起
zl[n] = sum;
}
while(cin >> n && n){
cout << zl[n] << endl;
}
return 0;
}

  

 

Statistic | Submit | Discuss | Note

71-n皇后的更多相关文章

  1. 54. 八皇后问题[eight queens puzzle]

    [本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即 ...

  2. 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后

    私人博客原文链接来自:http://www.hexcode.cn/article/show/eight-queen 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化 8皇后以 ...

  3. 八皇后问题 --- 递归解法 --- java代码

    八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上.八皇后 ...

  4. 题目---汉诺塔及AI代码及八皇后

    2019春第十一周作业 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering ...

  5. 算法实验5--N皇后

    实验名称 回溯法解N皇后问题 实验目的 掌握回溯递归算法.迭代算法的设计与实现: 设计回溯算法求解: 分析算法的时间复杂度. 实验环境 操作系统:win 10; 编程语言:Java: 开发工具:IDE ...

  6. leetcode 51. N皇后 及 52.N皇后 II

    51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...

  7. 递归实现n(经典的8皇后问题)皇后的问题

    问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...

  8. 八皇后算法的另一种实现(c#版本)

    八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...

  9. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  10. [LeetCode] N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

随机推荐

  1. java重置Timer执行频率

    public class BallUtil { public static Timer fisTimer ; public static void fisStartBall(){ long first ...

  2. 使用javah生成jni 头文件和使用ndk编译so库

    1.jni 首先clean Project,在makeProject生成对应的class文件 然后点出命名框,输入命令: cd app/build/intermediates/classes/debu ...

  3. linux 处理端口

    1.查看8080端口是否被占用: netstat -anp | grep 8080 2.查看占用8080端口的进程:fuser -v -n tcp 8080 3.杀死占用8080端口的进程: kill ...

  4. New Concept English three (53)

    30w/m 56errors The Scandinavian countries are much admired all over the world for their enlightened ...

  5. LeetCode Longest Continuous Increasing Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...

  6. C# XML反序列化与序列化举例:XmlSerializer

    using System; using System.IO; using System.Xml.Serialization; namespace XStream { /// <summary&g ...

  7. Nginx理解

    说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了.摘一段百度百科上的描述: 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后 ...

  8. BZOJ3110:[ZJOI2013]K大数查询(整体二分版)

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  9. Poj 3253 Fence Repair(哈夫曼树)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  10. NET代码运行在服务器JS运行在客户端

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;usi ...