8 Queens Chess Problem

八皇后问题详解(附有C++详细代码) 八皇后问题是一个经典的回溯算法问题,通过求解该问题,我们可以深入理解回溯算法的基本思想和应用方法。解决八皇后问题的关键在于找到一个有效的算法来搜索所有可能的摆放方式,并在搜索过程中进行剪枝,以避免不必要的搜索。在摆放过程中,需要判断当前位置是否合法,即是否与其他皇后冲突。该问题描述为:在8×8的国际象棋上摆放八个皇后,使其不能互相攻击,即任何两个皇后都不能处于同一行、同一列或同一斜线上。由于八皇后问题有92种不同的摆放方式,因此输出会包含92行,每行8个数字,表示一种摆放方案中每个皇后在棋盘上的列位置。 阅读详情

  8 Queens Chess Problem 

In chess it is possible to place eight queens on the board so that no one queen can be taken by any other. Write a program that will determine all such possible arrangements for eight queens given the initial position of one of the queens.

Do not attempt to write a program which evaluates every possible 8 configuration of 8 queens placed on the board. This would require 88evaluations and would bring the system to its knees. There will be a reasonable run time constraint placed on your program.

Input 

The first line of the input contains the number of datasets, and it's followed by a blank line. Each dataset will be two numbers separated by a blank. The numbers represent the square on which one of the eight queens must be positioned. A valid square will be represented; it will not be necessary to validate the input.

To standardize our notation, assume that the upper left-most corner of the board is position (1,1). Rows run horizontally and the top row is row 1. Columns are vertical and column 1 is the left-most column. Any reference to a square is by row then column; thus square (4,6) means row 4, column 6.

Each dataset is separated by a blank line.

Output 

Output for each dataset will consist of a one-line-per-solution representation.

Each solution will be sequentially numbered $1 \dots N$. Each solution will consist of 8 numbers. Each of the 8 numbers will be the ROW coordinate for that solution. The column coordinate will be indicated by the order in which the 8 numbers are printed. That is, the first number represents the ROW in which the queen is positioned in column 1; the second number represents the ROW in which the queen is positioned in column 2, and so on.

The sample input below produces 4 solutions. The full 8$\times$8 representation of each solution is shown below.

DO NOT SUBMIT THE BOARD MATRICES AS PART OF YOUR SOLUTION!

   SOLUTION 1           SOLUTION 2           SOLUTION 3           SOLUTION 4

1 0 0 0 0 0 0 0      1 0 0 0 0 0 0 0      1 0 0 0 0 0 0 0      1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0      0 0 0 0 0 0 1 0      0 0 0 0 0 1 0 0      0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0      0 0 0 1 0 0 0 0      0 0 0 0 0 0 0 1      0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1      0 0 0 0 0 1 0 0      0 0 1 0 0 0 0 0      0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0      0 0 0 0 0 0 0 1      0 0 0 0 0 0 1 0      0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0      0 1 0 0 0 0 0 0      0 0 0 1 0 0 0 0      0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0      0 0 0 0 1 0 0 0      0 1 0 0 0 0 0 0      0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0      0 0 1 0 0 0 0 0      0 0 0 0 1 0 0 0      0 0 0 1 0 0 0 0

Submit only the one-line, 8 digit representation of each solution as described earlier. Solution #1 below indicates that there is a queen at Row 1, Column 1; Row 5, Column 2; Row 8, Column 3; Row 6, Column 4; Row 3,Column 5; ... Row 4, Column 8.

Include the two lines of column headings as shown below in the sample output and print the solutions in lexicographical order.

Print a blank line between datasets.

Sample Input 

1

1 1

Sample Output 

SOLN       COLUMN
 #      1 2 3 4 5 6 7 8

 1      1 5 8 6 3 7 2 4
 2      1 6 8 3 7 4 2 5
 3      1 7 4 6 8 2 5 3
 4      1 7 5 8 2 4 6 3


#include<iostream>
#include<cstdio>
using namespace std;


int n;
void backtracking( int, int*, int*, int*, int*, int* );
int main()
{
    int N;
    int row, col, array[8];
    bool blank_line;
    while( scanf( "%d", &N ) != EOF )
    {
        blank_line = 0;
        for( int i = 0 ; i < N ; i++ )
        {
            scanf( "%d%d", &row, &col );
            row--, col--;
            if( blank_line ) printf( "\n" );
            printf( "SOLN       COLUMN\n" );
            printf( " #      1 2 3 4 5 6 7 8\n" );
            printf( "\n" );
            blank_line = 1;
            int rowput[8] = {0}, colput[8] = {0}, leftslash[15] = {0}, rightslash[15] = {0};
            rowput[row] = 1;
            colput[col] = 1;
            leftslash[row+col] = 1;
            rightslash[row-col+7] = 1;
            n = 0;
            array[col] = row;
            backtracking( 0, array, rowput, colput, leftslash, rightslash );
        }
    }
    return 0;
}


void backtracking( int i, int array[], int rowput[], int colput[], int leftslash[] , int rightslash[] )
{
    if( i == 8 )
    {
        printf( "%2d      ", ++n );
        for( int j = 0 ; j < 8 ; j++ )
        {
            if( j ) printf( " " );
            printf( "%d", array[j]+1 );
        }
        printf( "\n" );
        return;
    }
    if( colput[i] )
    {
        backtracking( i+1, array, rowput, colput, leftslash, rightslash );
        return;
    }
    for( int j = 0 ; j < 8 ; j++ )
    {
        if( rowput[j] || leftslash[j+i] || rightslash[j-i+7] )
            continue;
        rowput[j] = 1;
        leftslash[i+j] = 1;
        rightslash[j-i+7] = 1;
        array[i] = j;
        backtracking( i+1, array, rowput, colput, leftslash, rightslash );
        rowput[j] = 0;
        leftslash[i+j] = 0;
        rightslash[j-i+7] = 0;
    }
}


8 Queens Problem (八皇后问题) The goal of 8 Queens Problem is to put eight queens on a chess-board such that none of them threatens any of others. A queen threatens the squares in the same row, in the same column, or on the same ... 阅读详情

相关推荐

第十九章 ALDS1_13_A:8 Queens Problem 八皇后问题

知识点 八皇后问题比较多解法,这里说的是最简单的回溯解法。 问题链接ALDS1_13_A:8 Queens Problem问题内容 在8*8的国际象棋棋盘里,有k个皇后已经放好了,皇后会将她的这行、这列、左右斜边上的其他棋子攻击,问如何将8个皇后放到8*8的棋盘保证她们互相不攻击。 思路 我们用递归的方式去尝试,在递归完成后若不成功,则恢复原来的状态继续尝试其他的递归。 代码#inclu

小酷Miki的专栏 722

2021-1-25 八皇后问题(DFS解法)(递归)

一.例题: The goal of 8 Queens Problem is to put eight queens on a chess-board such that none of them threatens any of others. A queen threatens the squares in the same row, in the same column, or on the same diagonals as shown in the following figure. For a g

m0_52378877的博客 284

EightQueensProblem:用爬山法解决八皇后问题

八皇后问题 用爬山法解决八皇后问题 import java.util.ArrayList; import java.util.List; import java.util.Random; public class ChessBoard { public static int Q= 8; private int[][] board; private int[] queenPositions; public static void main(String[] args) { boolean climb = true; int climbCount = 0; while (climb) { ChessBoard board = new ChessBoard( new int[Q][Q], new int[8]); board.placeQueens()

UVa 750 - 8 Queens Chess Problem

题目:计算一定经过给定点的八皇后。 分析:搜索。因为八皇后只有92组解,直接计算出92组解,然后查询输出即可。             这里我使用了位运算来计算八皇后,减少代码量。             先考虑一个皇后的影响,每次下一层攻击的点和上一次的关系如下:             一个皇后会影响自己下方和左右两个斜的方向(从上往下搜索);             向左的斜的

小白菜又菜 4564

第一百题 UVAa750 8 Queens Chess Problem 目之所及 皆是遗憾

终于写够一百道题啦,怎么也没想过他是这样来临的,在一个阳光明媚的冬日,写完第九十九题之后还是非常激动的,不知道第一百题做什么好,ACM群里不知怎么的就讨论起了八皇后问题(好像还是因为我发了一个非常奇怪的问题求解答)我犹豫了三分钟之后,算了,干脆写个八皇后问题吧,这题很熟悉,但是现在却已经都忘记了题目是什么样子的,又翻了出来。。。。 In chess it is possible to place eight queens on the board so that no one queen can be taken by any other. Write a program that will

[工具使用]黑暗引擎FOFA

黑暗引擎FOFAFOFA(点我进入)逻辑运算符搜索子域名domain搜索指定内容的host全部域名bodycert搜索选定应用的网站搜索指定开放端口的IP搜索指定协议的IP搜索IP或者网段的信息搜索指定CSS/JS网站常用的一些内容其他 FOFA(点我进入) 官网这样描述:FOFA是白帽汇推出的一款网络空间资产搜索引擎。它能够帮助用户迅速进行网络资产匹配、加快后续工作进程。例如进行漏洞影响范围分析、应用分布统计、应用流行度排名统计等。 简单理解就是,一个本土加强版shodan,知道某产品在互联网的部署情况、

网络安全知识分享 3万+

八皇后 Heuristic Search - 8 Queens Problem

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_A Heuristic Search - 8 Queens Problem Time Limit : 1 sec, Memory Limit : 131072 KB 8 Queens Problem The goal of 8 Queens Probl...

沉默的大多数 1106

RLC电阻电容电感基础知识——电感篇

电感 等效电路 电感线圈由铜导线绕制,等效电路如图所示。 寄生电阻RS,包括了绕线电阻、引脚串联电阻以及磁芯损耗电阻三部分组成,其中绕线电阻与线径、长度、铜线电阻率有关;引脚电阻与引线长短、粗细有关;磁芯损耗电阻与磁芯材料特性有关,显然磁芯损耗电阻还与工作频率有关.寄生电容C主要是绕线圈与圈之间的杂散电容(为PF级),与绕线工艺有关。 等效导纳 显然: (1)当角频率ω(频率f)较小时,呈电感性. (2)当角频率ω等于ω0时,导纳Y虚部为0,呈现电阻性,即发生共振。令导纳Y虚部为0,即可求出共振角频率ω

azj2019的博客 1万+

UVa 750 8 Queens Chess Problem

本题通过预先生成所有八皇后解,再根据固定坐标筛选,避免了在查询时重复回溯,提高了效率。关键在于回溯时使用列、对角线的冲突检测,并保存解的矩阵表示,便于快速检索。输出时注意按字典序排列行号序列,并严格遵循输出格式。该解法简洁且高效,适用于多组查询。八皇后问题的经典回溯框架是本题的核心,筛选和排序是应用层面的扩展。

寂静山林 200

IFRS 9与CECL对比解析:跨国企业财务必须掌握的两种信用损失模型

本文深度解析IFRS 9与CECL两种信用损失模型的差异,帮助跨国企业应对财务合规挑战。从基础架构、数据治理到监管策略,详细对比三阶段模型与单一阶段模型的核心区别,并提供实施双轨制的实务建议,助力企业优化资本管理和税务筹划。

3c4x5z6v7b的博客 668

静态时序分析—门控时钟检查(clock gating check)

clock gating check的条件与类型

m0_61544122的博客 9606

UVA 750 - 8 Queens Chess Problem

经典的八王后问题,回溯算法代码如下:#include &lt;bits/stdc++.h&gt; using namespace std; int number=0,x[10],y[100][10]; void D()//存储王后位置 { int i; for(i =1; i &lt;=8; i++) y[number][i]=x[i]; number...

liuxinyu666的博客 814

Android11车机系统海外项目资源包RRO方案优化

本文介绍了汽车设置应用如何通过Android RRO(运行时资源叠加)方案实现多车型差异化资源管理。针对不同车型(国内/海外、轿车/SUV)和配置(方向盘、轮毂等),采用产品变体(productFlavors)构建不同APK版本,并通过JSON配置文件动态启用对应资源包。系统根据车辆下线配置自动匹配适用的资源叠加包,解决了硬编码判断带来的维护问题。该方案实现了资源的高效复用,同时支持灵活配置多种车型组合,为汽车行业多车型共平台开发提供了可扩展的技术实现。

shusuanly的博客 469

UVA750 【8 Queens Chess Problem】 (递归与回溯、8皇后问题)

链接 UVA750-8 Queens Chess Problem 题目描述: In chess it is possible to place eight queens on the board so that no one queen can be taken by any other. Write a program that will determine all such possible arrangements for eight queens given the initial position

Obliviate的博客 394

【原创】下载RealEstate10K数据集原始视频的方法

目前互联网上能搜到下载RealEstate10K数据集原始视频的方法都已经不能用了,这篇博客介绍一种目前可用的下载RealEstate10K数据集原始视频的方法,并给出自动化的脚本代码。

沉迷单车的追风少年 2461

《拉依达的嵌入式\驱动面试宝典》—Linux篇(八)_Linux驱动编程

《拉依达的嵌入式\驱动面试宝典》 整合了所有我认为可能遇到的技术面试问题。所有问题及其答案均是我个人学习后查阅资料总结,每个回答都做了仔细的分析,在嵌入式及其驱动面试相关问题解析做到全网独一无二。学习或者转行到嵌入式、驱动开发领域学习的不二之选

拉依达不拉跨的博客 319

UVA750 UVALive5358 8 Queens Chess Problem题解

代码来源:DeathYmzAC的C++语言程序如下:#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; //很坑很坑的输出 !!!!! using namespace std; const int N=8; int n,top; int x,y; int C[N],vis[3][2*N]; void s...

海岛Blog 1397

如何对EMB电子机械制动器的热衰退问题进行优化

摘要 电子机械制动器(EMB)在新能源汽车和自动驾驶中具有重要应用价值,但其在高强度工况下会出现热衰退现象。本文分析了EMB热衰退的五大机理:摩擦副热效应、电机热衰退、减速机构热失效、温度对控制策略干扰以及结构设计导致的热积累。针对这些问题,提出了从材料、结构、控制三个维度的优化措施:升级耐高温材料、改进散热设计、优化控制算法等。通过"热生成抑制-热传导优化-热失效补偿"的系统方案,可有效改善EMB的热衰退问题,为智能汽车提供更可靠的制动保障。

m0_59012156的博客 342

2018_1_25_8 Queens Chess Problem_打表

https://vjudge.net/problem/UVA-750 注意格式,行列对应,回溯打表 #include #include #include #include #include #include using namespace std; int P[100][9]; int tmp[8]; int n=0; bool col[8]={0},lleft[15]={0},rrig

ujn20161222的博客 537

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

8 Queens Chess Problem UVA - 750 (很久之前WA,,然后 现在改了一点之后 PE,,又改了一会A了)

PEIn chess it is possible to place eight queens on the board so that no one queen can be taken by any other. Write a program that will determine all such possible arrangements for eight queens given t...

❂ 514

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

UVA-750-8-Queens-Chess-Problem(皇后问题)-回溯与递归

UVA-750-8-Queens-Chess-Problem参考题解,参考算法为回溯法,本文简要介绍递归与回溯法的核心原理以及它们之间的区别,并且回溯是类皇后问题的基本解法。

我是懒狗━┳━ ━┳━ 912

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

8 Queens Problem

The goal of 8 Queens Problem is to put eight queens on a chess-board such that none of them threatens any of others. A queen threatens the squares in the same row, in the same column, or on the same ...

ML-M的博客 707
上一篇: 给浮躁的软件业同仁
下一篇: (孙鑫)VC++深入讲解 第八章 VS2010实现逃跑按钮
i丹明扬
博客等级 码龄15年 331粉丝 115原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值