Aizu - ALDS1_13_A-8 Queens Problem-八皇后的路径输出
The goal of 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 given chess board where kk queens are already placed, find the solution of the queens problem.
Input
In the first line, an integer kk is given. In the following kk lines, each square where a queen is already placed is given by two integers rr and cc. rr and cc respectively denotes the row number and the column number. The row/column numbers start with . Output
Print a ×× chess board by strings where a square with a queen is represented by 'Q' and an empty square is represented by '.'. Constraints
There is exactly one solution
Sample Input 3
Sample Output
......Q.
Q.......
..Q.....
.......Q
.....Q..
...Q....
.Q......
....Q...
题意:
给出n,代表给出n个确定的棋子数坐标,输出任意一组满足条件的八皇后即可。
不能直接根据斜率进行判断
不知道为什么???
int judge(int x)
{
for(int i=; i<x; i++) //判断之前和走过的行是否有重复
{
int dd1=abs(i-x);
int dd2=abs(a[i]-a[x]);
if(dd1==dd2)
{
return ;
}
}
return ;
//对角线出现过即k=-1或1
//即斜率的绝对值=1
//即两者的横纵坐标对应相减后绝对值相等
}
dfs不知道怎么解释,详细解释看代码上的注释吧。
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>
using namespace std; int a[];//记录第几行第几列下了棋
int aa[];//标记行
char s[][];//存图,记录输出
int b[],c[],d[];//标记列、斜率>0的对角线、斜率<0的对角线
//对角线需要开的大一点
int flag; //int judge(int x)//传入行
//{
// for(int i=0; i<x; i++) //判断之前和走过的行是否有重复
// {
// int dd1=abs(i-x);
// int dd2=abs(a[i]-a[x]);
// if(dd1==dd2)
// return 1;
// }
// return 0;
// //对角线出现过即k=-1或1
// //即斜率的绝对值=1
// //即两者的横纵坐标对应相减后绝对值相等
//}
//不知道为什么对角线这样判断会WA void dfs(int x)//传入行
{
if(flag==)
return ;
if(x>)
{
flag=;//因为只需要输出一组结果
for(int i=; i<; i++)
printf("%s\n",s[i]);
return ;
} if(aa[x])//如果该行标记过
dfs(x+);//则往下一行进行搜索
else
{
//如果该行未被标记过,则在该行进行下棋
for(int j=; j<; j++) //决定下在哪一列
{
//a[x]=j;//下上去
//if(b[j]==0&&(judge(x)==0)) //如果该列没有标记且两条对角线没有标记过
if(b[j]==&&c[x+j]==&&d[x-j+]==)
{
c[x+j]=;
d[x-j+]=;
b[j]=;
s[x][j]='Q';
dfs(x+);
s[x][j]='.';
b[j]=;
c[x+j]=;
d[x-j+]=;
}
}
}
return ;
} int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int t;
while(cin>>t)
{
memset(a,,sizeof(a));
memset(aa,,sizeof(aa));
memset(b,,sizeof(b));
memset(c,,sizeof(c));
memset(d,,sizeof(d));
flag=;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
s[i][j]='.';
}
int x,y;
for(int i=; i<t; i++)
{
cin>>x>>y;
c[x+y]=;
d[x-y+]=;//标记对角线
aa[x]=;//标记行
a[x]=y;//该点确定下棋了
b[y]=;//标记列
s[x][y]='Q';//确定改点需要输出
}
dfs();
}
return ;
}
Aizu - ALDS1_13_A-8 Queens Problem-八皇后的路径输出的更多相关文章
- Python----递归------Eight Queens 八皇后问题
递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个 ...
- 54. 八皇后问题[eight queens puzzle]
[本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即 ...
- C语言解决八皇后问题
#include <stdio.h> #include <stdlib.h> /* this code is used to cope with the problem of ...
- Python学习二(生成器和八皇后算法)
看书看到迭代器和生成器了,一般的使用是没什么问题的,不过很多时候并不能用的很习惯 书中例举了经典的八皇后问题,作为一个程序员怎么能够放过做题的机会呢,于是乎先自己来一遍,于是有了下面这个ugly的代码 ...
- Python解决八皇后问题
最近看Python看得都不用tab键了,哈哈.今天看了一个经典问题--八皇后问题,说实话,以前学C.C++的时候有这个问题,但是当时不爱学,没搞会,后来算法课上又碰到,只是学会了思想,应该是学回溯法的 ...
- 八皇后,回溯与递归(Python实现)
八皇后问题是十九世纪著名的数学家高斯1850年提出 .以下为python语句的八皇后代码,摘自<Python基础教程>,代码相对于其他语言,来得短小且一次性可以打印出92种结果.同时可以扩 ...
- 回溯算法-C#语言解决八皇后问题的写法与优化
结合问题说方案,首先先说问题: 八皇后问题:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 嗯,这个问题已经被使用各种语言解 ...
- 八皇后问题详细分析与解答(递归法解答,c#语言描述)
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或 ...
- 【Python】生成器、回溯和八皇后问题
八皇后问题: 把N个皇后,放在N*N的棋盘上面,从第一行往下放,每个皇后占一行,同时,每个皇后不能处在同一列,对角线上,有多少种放置方法. 思路: 典型的回溯问题: 1.当要放置最后一个皇后时候,默认 ...
随机推荐
- Android Studio androidx 包冲突解决方法
如果包冲突了会包如下这样的错: Android dependency 'androidx.core:core' has different version for the compile (1.0.0 ...
- mybatis的核心对象图解
- date和string转换用joda包
import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format ...
- SonarQube搭建和使用教程
我想使用 SonarQube 查阅代码 请问怎么做,现在只有一个要审查代码的项目
- SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2498.html 题目大意 略. 分析 ...
- 1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- 1067 Sort with Swap(0, i) (25 分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- 拾遗:Qemu/KVM
WinXP: #!/bin/bash name=winxp qemu-system-x86_64 \ -enable-kvm \ -cpu host -smp ,sockets=,cores=,thr ...
- Spring 学习笔记 Resource 资源
Spring Resources 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理 URL 资源.File 资源.ClassPath相关资源等等.并且在 java 中 Java . ...
- js实现禁止页面拖拽图片
document.ondragstart = function() { return false;};