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

第一百题 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 立即下载

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 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 88 evaluations 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 contains a pair of positive integers separated by a single space. 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 leftmost 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 . . . 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. Notes: The sample input below produces 4 solutions. The full 8×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. 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>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8;
int a[10][10] = { 0 }, b[99][9];
int t = 1;
void print()
{
    //   printf("No. %d\n", t++);
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if(a[i][j])
                b[t][i] = j+1;
            // printf("%d ", a[j][i]);
        }
        //      printf("\n");
    }
    t++;
}
bool judge(int x, int y)
{
    for(int i=0; i<x; i++)
    {//竖直方向||左上方 || 右上方
        if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))
            return false;
    }
    return true;
}
/*
void dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
       if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            dfs(num + 1);
            a[num][i] = 0;
        }
    }
}
*/

int dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
        if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            if(dfs(num + 1))
                return 1;
            a[num][i] = 0;
        }

    }
    return 0;
}
int main()
{


    dfs(0);
    /*
    for(int i=1; i<93; i++)
        for(int j=0; j<8; j++) {
            cout << b[i][j] << " ";
            if (j == 7)

        cout << endl;}*/

    int n, x, y;

    cin >> n;



    while(n--) {
        cin >> x >> y;
        int t = 1;
        printf("SOLN       COLUMN\n");
        printf(" #      1 2 3 4 5 6 7 8\n\n");
        int i, j = 0;
        for (i = 1; i < 93; i++) {
/*            if (j == 8) {
                j = 0;
                cout << endl;
            }
*/
            //  if(b[i][0] >= x && b[i][0] <= y)
            if (b[i][y - 1] == x) {
                cout << setw(2) << t++ << "     ";
                for (j = 0; j < 8; j++) {

                    cout << " " << b[i][j];


                }
                cout << endl;
                    //else t = 1;
            }

        }
        if(n){
            cout << endl;


        }

    }
    return 0;
}

//WA版本 ,,,WA的原因是因为理解错题意,,,以为第一个数字在x 和y之间就可以了,,重看发现x,y确定了一个坐标.......

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8;
int a[10][10] = { 0 }, b[99][9];
int t = 1;
void print()
{
 //   printf("No. %d\n", t++);
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if(a[i][j])
                b[t][i] = j+1;
           // printf("%d ", a[j][i]);
        }
  //      printf("\n");
    }
    t++;
}
bool judge(int x, int y)
{
    for(int i=0; i<x; i++)
    {//竖直方向||左上方 || 右上方
        if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))
            return false;
     }
    return true;
}
/*
void dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
       if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            dfs(num + 1);
            a[num][i] = 0;
        }
    }
}
*/

int dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
        if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            if(dfs(num + 1))
                return 1;
            a[num][i] = 0;
        }

    }
return 0;
}
int main()
{


    dfs(0);
    /*
    for(int i=1; i<93; i++)
        for(int j=0; j<8; j++) {
            cout << b[i][j] << " ";
            if (j == 7)

        cout << endl;}*/

    int n, x, y;

    cin >> n;

    printf("SOLN       COLUMN\n");
    printf(" #      1 2 3 4 5 6 7 8\n\n");

    while(n--)
    {
        cin >> x >> y;
        int t = 1;
        for(int i=1; i<93; i++) {

            if(b[i][0] >= x && b[i][0] <= y)
            {
                cout << setw(2) << t++ << "     ";
                for (int j = 0; j < 8; j++) {

                cout << " " << b[i][j];
                if (j == 7)

                    cout << endl;
            }

            }else t = 1;
        }


    }
    return 0;
}

 

//PE版本


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8;
int a[10][10] = { 0 }, b[99][9];
int t = 1;
void print()
{
    //   printf("No. %d\n", t++);
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if(a[i][j])
                b[t][i] = j+1;
            // printf("%d ", a[j][i]);
        }
        //      printf("\n");
    }
    t++;
}
bool judge(int x, int y)
{
    for(int i=0; i<x; i++)
    {//竖直方向||左上方 || 右上方
        if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))
            return false;
    }
    return true;
}
/*
void dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
       if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            dfs(num + 1);
            a[num][i] = 0;
        }
    }
}
*/

int dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
        if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            if(dfs(num + 1))
                return 1;
            a[num][i] = 0;
        }

    }
    return 0;
}
int main()
{


    dfs(0);
    /*
    for(int i=1; i<93; i++)
        for(int j=0; j<8; j++) {
            cout << b[i][j] << " ";
            if (j == 7)

        cout << endl;}*/

    int n, x, y;

    cin >> n;



    while(n--) {
        cin >> x >> y;
        int t = 1;
        printf("SOLN       COLUMN\n");
        printf(" #      1 2 3 4 5 6 7 8\n\n");
        int i, j = 0;
        for (i = 1; i < 93; i++) {
/*            if (j == 8) {
                j = 0;
                cout << endl;
            }
*/
            //  if(b[i][0] >= x && b[i][0] <= y)
            if (b[i][y - 1] == x) {
                cout << setw(2) << t++ << "     ";
                for (j = 0; j < 8; j++) {

                    cout << " " << b[i][j];


                }
                //else t = 1;
            }
            cout << endl;
        }
        if(n){
            cout << endl;
        }

    }
    return 0;
}

 

STM32F103VET6实战:用W25Q64外部Flash实现Bootloader+Application双备份(含完整代码解析) 本文详细介绍了基于STM32F103VET6微控制器和W25Q64外部Flash实现Bootloader+Application双备份固件升级的完整方案。通过精心设计的Flash分区策略、可靠的Bootloader程序以及包含双重校验和自动回滚机制的升级流程,有效解决了远程升级过程中断电导致设备变砖的风险,确保了工业嵌入式系统的高可靠性与稳定性。 阅读详情

相关推荐

Acgis中实现栅格经纬度和行政区关联

目前已有意大利的shp文件,希望将意大利划分成0.1*0.1经纬度的栅格,并且关联每个栅格中心点所属的省份信息。最终希望得到的输出是一个表格,里面包含栅格的经纬度、所属省份信息。

qq_42183184的博客 3369

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

qq_41486817的博客 902

PFC胶结模型实战:从线性到平行粘结的力学行为解析

本文深入解析了PFC中线性接触与平行粘结(pb模型)的核心差异与应用。平行粘结模型通过模拟胶结层的力学行为,能够有效传递拉力和抵抗弯矩,是模拟岩石、混凝土等胶结材料的关键。文章详细介绍了其变形与强度参数的设置、法向与切向行为的验证方法,并揭示了全量与增量力计算中易出现的“幽灵力”问题及其解决方案,为复杂模型的构建与调试提供了实用策略。

weixin_29230773的博客 1207

UVA 750 八皇后·(2)+ 预处理+递归

#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;set&gt; #include&lt;map&gt; #include&lt;queue&gt; #include&lt;ctype.h&gt; #include&

兜率工的博客 406

鸿蒙子系统解读-分布式软总线子系统初步研究(下)

鸿蒙子系统解读-分布式软总线子系统初步研究(下) 本文作者:江苏润和软件股份有限公司 郎建中 3.2. 数据传输 上一节我们看了PublishService()函数的代码实现过程。被发现端发布服务后,软总线回调发布成功的函数中,被发现端需要使用CreateSessionServer()来创建会话的服务器等待发现端的连接创建会话。下面我们就来分析CreateSessionServer()函数的实现过程。 数据传输部分的代码位于foundation/communication/services/softbus

weixin_47070198的博客 3088

新手必看:LiteRadio 2c SE遥控器从开箱到飞行的完整指南(附常见问题解答)

本文为新手提供LiteRadio 2c SE遥控器的完整使用指南,涵盖从开箱到首飞的20个关键步骤。详细介绍了遥控器的功能设置、与无人机的对频技巧以及首飞注意事项,帮助用户快速掌握飞行控制,避免常见错误。特别适合无人机初学者阅读。

weixin_29209801的博客 380

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

京东前端200道面试题及参考答案(中)

前端优化是“提升用户体验、降低资源消耗”的核心手段,需围绕“用户感知的性能指标”(如页面加载速度、交互流畅度)展开,覆盖加载优化、渲染优化、代码优化、网络优化、缓存优化、性能监控六大维度,每个维度下有具体可落地的措施,同时需结合项目实际场景(如单页应用、电商首页)选择优化方案。分页组件需通过props接收“数据总量、当前页、每页条数”等基础信息,通过emit触发“页码切换、每页条数变化”的回调(供父组件请求对应页数据),同时定义默认值确保兼容性。核心Props与Emit设计<template>

大模型大数据攻城狮的专栏 590

UVa 750 - 8 Queens Chess Problem

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

小白菜又菜 4564

OpenClaw实战系列·进阶特辑02:多智能体协同——打造企业级AI中台

**企业级AI中台面临多Agent协同的挑战,本文提出基于多智能体协同的解决方案。通过分析单Agent的局限性(上下文溢出、专业度稀释等)和多Agent的核心优势(任务专注度、故障隔离等),提出三种协作架构模式(指挥型、联邦型、点对点型),并强调人格化设计的重要性。文章详细介绍Agent间通讯机制(同步/异步会话、文件系统协作)和"身脑分离"的解耦架构设计,提供可落地的企业级AI中台实施方案。

PeterPan的博客 494

14 硬件工程师笔面试高频考点真题解析——逻辑门

逻辑门是数字电路的基本单元,实现布尔运算(如与、或、非、异或等)。OC门(集电极开路)和OD门(漏极开路)需外接上拉电阻,支持“线与”逻辑,适用于I2C总线等场景,避免推挽输出并联导致的短路问题。三态门具有高、低、高阻三种状态,用于总线分时复用。推挽输出因内置上下管,禁止并联,否则会引发电源短路。三者区别在于:推挽驱动强但不可并联;OC/OD需上拉,支持线与;三态门通过使能控制实现高阻隔离,适用于共享总线设计。

XU157303764的博客 1902

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

JNI的本质解析:Android Framework视角下的Java-Native衔接机制

本文基于Android 14源码深入分析JNI机制,从Android Framework视角解析Java-Native衔接原理。主要内容包括:JNI的双轨制注册机制(静态与动态注册),核心数据结构(JNIEnv与JavaVM)的实现原理,以及完整的调用链路剖析。文章通过MessageQueue的JNI实现案例,展示Android Framework如何利用JNI实现Java层与Native层的高效交互,并提供了性能优化建议。分析表明,JNI作为Android系统架构的关键桥梁,其设计充分考虑了性能、安全性

保持专注,保持热爱 1210

UVA 750 八皇后·(2)+ 预处理+递归+棋盘问题

1 题意,题目描述里似乎没说清楚输入输出格式。 2 分析。 学习:用一个行数组、左对角线数组、右对角线数组,分别表示某一行、某一条左对角线个、某一条右对角线是否被占用。用于高效的处理,辨别是否在同一行、同一列、同一对角线的情况! 3 #include #include #include using namespace std; int cnt=0; int p[110][9];//

他的博客 713

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

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

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

JAVA三星题之Eight Queens

public class Queen { public static final int size=8;//棋盘的大小,也表示皇后的数目 public static int[] location=new int[size];//皇后在期盼的每行上的列的位置 public static boolean[] colsOccupied = new boolean[size];//皇后在棋盘上占据的

想去海邊 1184

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

uva 750 八皇后问题

点击打开链接//八皇后问题 #include&lt;bits/stdc++.h&gt; using namespace std; int r,c,C[10],ans; bool done[3][30]; void dfs(int cur) { if(cur==9) { ans++; printf("%2d ",ans); for(in...

老铁,干了这碗algorithms的博客 800

PAT - 甲级 - 1128. N Queens Puzzle (20) (8皇后问题变形)

题目描述: The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the s

i逆天耗子 2233

PAT--1128. N Queens Puzzle

The “eight queens puzzle” is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, c

许少y 1289

Eight Queens UVa 750

当时就觉得回溯法学的不是很好,在处理dp多状态问题前,温习一遍回溯法:把待求解的问题分成不太多的步骤,每个步骤只有不太多的选择,就可以考虑回溯法。 普通八皇后思路:从64格子中选8个格子,枚举的话一共有$C^{8}_{64}=4.426X10^{9}$解法,不爆才怪。观察基本性质,每一行只能有一个皇后,所以最多只有$8!$种枚举可能。在实际操作的时候,状态树伸展肯定比这个值还要小,因为和之前的状...

weixin_30415801的博客 153

八皇后 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

uva 167 The Sultan's Successors(回溯)

The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate parts on her death and each part will be inherited

不慌不忙、不急不躁 2338

eight queens

Question: Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal. Answer: #include using namespace std; int

行风 667

Doubles POJ 1552, ZOJ 1760, UVA 2787.

点击打开链接As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list are ...

❂ 567
上一篇: The Sultan's Successors UVA - 167
下一篇: 判决素数个数 OpenJ_Bailian - 3177
KLFTESPACE
博客等级 码龄9年 66粉丝 655原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值