69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include <math.h>
#define SUCCESS 1
#define FAIL 0
#define STATE bool
const int Queen = 5;
int column[Queen];
int number=0;
//输出函数
void Output()
{
++number;
printf("解法编号:%d\n",number);
for (int i=0;i<Queen;++i)
{
for (int j=0;j<Queen;++j)
{
if (column[i]-1==j)
{
printf(" Q");
}
else
printf(" .");
}
printf("\n");
}
printf("\n\n");
}
STATE IsValid(int num)
{
for(int i=0;i<num;++i)
{
//检查是否同列
if (column[i]==column[num])
{
return FAIL;
}
//检查是否同在一条斜线上
if (abs(column[i]-column[num])==(num-i))
{
return FAIL;
}
}
return SUCCESS;
}
//疑问:到底是怎么输出好几个结果的?
//我感觉输出一种结果就会结束这个函数啊
void EightQueen(int num)
{
//如果找到了一种解法,则输出
if (num==Queen)
{
Output();
}
else
{
for (int col=1;col<=Queen;++col)
{
column[num] = col;
if (IsValid(num))
{
EightQueen(num+1);
}
}
}
}
void main()
{
EightQueen(0);
}
/**
* The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
*
* Given an integer n, return all distinct solutions to the n-queens puzzle.
*
* Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
*
* For example,
* There exist two distinct solutions to the 4-queens puzzle:
*
* [
* [".Q..", // Solution 1
* "...Q",
* "Q...",
* "..Q."],
*
* ["..Q.", // Solution 2
* "Q...",
* "...Q",
* ".Q.."]
* ]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
void print_solves(vector<vector<string> > &vec) {
printf("[\n");
for (int i = 0; i < vec.size(); ++i) {
printf(" [\n");
for (int j = 0; j < vec[i].size(); ++j) {
printf("%s\n", vec[i][j].c_str());
}
printf(" ]\n");
}
printf("]\n");
}
class Solution {
public:
int diff(int x, int y) {
return x > y ? x - y : y - x;
}
int succ(int index) {
for (int i = 1; i < index; ++i) {
if (array_[index] == array_[i] || (diff(index, i) == diff(array_[index], array_[i]))) {
return 0;
}
}
return 1;
}
void solve_n_queens(int index) {
if (index > n_) {
vector<string> vec;
for (int i = 0; i < n_; ++i) {
string str(n_, '.');
str[array_[i + 1] - 1] = 'Q';
vec.push_back(str);
}
result_.push_back(vec);
return;
}
for (int i = 1; i <= n_; ++i) {
array_[index] = i;
if (succ(index)) {
solve_n_queens(index + 1);
}
}
}
vector<vector<string> > solveNQueens(int n) {
n_ = n;
array_.resize(n + 1, 0);
solve_n_queens(1);
return result_;
}
private:
int n_;
vector<int> array_;
vector<vector<string> > result_;
};
int main(int argc, char **argv) {
int i = 8;
if (argc == 2) {
i = atoi(argv[1]);
}
Solution s;
//for (int i = 0; i < 9; ++i) {
vector<vector<string> > ret = s.solveNQueens(i);
printf("%d size:%d.\n", i, ret.size());
//}
print_solves(ret);
return 0;
}