gpt4 book ai didi

c++ - N 皇后解决方案 C++

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:47 24 4
gpt4 key购买 nike

所以我需要帮助解决经典的 N-Queens 问题。

运行程序的命令是:nqueens N k - 其中 N 是表的大小 (N x N),k 是解决方案的数量

因此,例如,如果我通过键入 nqueens 4 1 运行该程序,将打印出以下内容。

_ 问 _ _

_ _ _ 问

问 _ _ _

_ _ 问 _

但是,我不知道如何处理超过 1 个解决方案?我如何才能确定此问题的不止一种解决方案?

目前我所拥有的如下:

#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <vector>


using namespace std;

class Board
{
private:
bool** spaces;
int size;

public:
Board(int size)
{
this->size = size;
spaces = new bool*[size];
for (int i = 0; i < size; ++i)
{
spaces[i] = new bool[size];
}
}

bool isSafe(int row, int column, vector<int>& state)
{
//check row conflict
//no need to check for column conflicts
//because the board is beimg filled column
//by column
for(int i = 0; i < column; ++i)
{
if(state[i] == row)
return false;
}

//check for diagonal conflicts
int columnDiff = 0;
int rowDiff = 0;
for(int i = 0; i < column; ++i)
{
columnDiff = abs(column - i);
rowDiff = abs(row - state[i]);
if(columnDiff == rowDiff)
return false;
}

return true;

}

int getSize()
{
return size;
}

bool getSpace(int x, int y)
{
return spaces[y][x];
}

void setSpace(int x, int y, bool value)
{
spaces[y][x] = value;
}

Board(Board& other)
{
this->size = other.getSize();
spaces = new bool*[size];
for (int i = 0; i < size; ++i)
{
spaces[i] = new bool[size];
for (int j = 0; j < size; ++j)
{
spaces[i][j] = other.getSpace(j, i);
}
}
}

void backtrack(vector<int>& state, int board_size)
{
int row = 0;
int column = 0;
bool backtrack = false;

while(column < board_size)
{
while(row < board_size)
{
if(backtrack)
{
row = state[column] + 1;
if(row == board_size)
{
column--; //backtrack more
backtrack = true;
row = 0;
break;
}

backtrack = false;
}

if(isSafe(row, column, state))
{
state[column] = row;
row = 0;
column++; //advance
backtrack = false;
break;
}

else
{
if(row == (board_size - 1))
{
column--; //backtrack
backtrack = true;
row = 0;
}
else
{
row++;
}
}
}
}
}
};

int print_solutions(Board *board, vector<int>& state, int board_size)
{
for(int i=0; i < board_size; ++i)
{
for(int j=0; j < board_size; ++j)
{
if(state[i] == j)
cout << 'Q' << " ";
else
cout << '_' << " ";
}

cout << endl;
}
}

int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: nqueens [Board Size] [Number of Solutions]" << endl;
return 1;
}

int board_size = atoi(argv[1]);
//int solution_count = atoi(argv[2]);
vector<int> state;
state.resize(board_size);

Board *my_board = new Board(board_size);
my_board->backtrack(state, board_size);

print_solutions(my_board, state, board_size);

return 0;
}

最佳答案

在这个解决方案中,我保留了大部分原始方法和代码:

#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <vector>
#include <iostream>

using namespace std;

class Board
{
private:
bool** spaces;
int size;

public:
Board(int size)
{
this->size = size;
spaces = new bool*[size];
for (int i = 0; i < size; ++i)
{
spaces[i] = new bool[size];
}
}

bool isSafe(int row, int column, vector<int>& state)
{
//check row conflict
//no need to check for column conflicts
//because the board is beimg filled column
//by column
for(int i = 0; i < column; ++i)
{
if(state[i] == row)
return false;
}

//check for diagonal conflicts
int columnDiff = 0;
int rowDiff = 0;
for(int i = 0; i < column; ++i)
{
columnDiff = abs(column - i);
rowDiff = abs(row - state[i]);
if(columnDiff == rowDiff)
return false;
}

return true;

}

int getSize()
{
return size;
}

bool getSpace(int x, int y)
{
return spaces[y][x];
}

void setSpace(int x, int y, bool value)
{
spaces[y][x] = value;
}

Board(Board& other)
{
this->size = other.getSize();
spaces = new bool*[size];
for (int i = 0; i < size; ++i)
{
spaces[i] = new bool[size];
for (int j = 0; j < size; ++j)
{
spaces[i][j] = other.getSpace(j, i);
}
}
}

bool backtrack(vector<int>& state, int& column, int board_size)
{
int row = 0;
bool backtrack = column == board_size;

while(column < board_size || backtrack)
{
{
if(backtrack)
{
if (column == 0)
return false;
column--;
row = state[column] + 1;
if(row == board_size)
{
backtrack = true;
continue;
}

backtrack = false;
}

if(isSafe(row, column, state))
{
state[column] = row;
row = 0;
column++; //advance
backtrack = false;
continue;
}

else
{
if(row == (board_size - 1))
{
backtrack = true;
}
else
{
row++;
}
}
}
}
return true;
}
};

void print_solutions(Board *board, vector<int>& state, int board_size)
{
for(int i=0; i < board_size; ++i)
{
for(int j=0; j < board_size; ++j)
{
if(state[i] == j)
cout << 'Q' << " ";
else
cout << '_' << " ";
}

cout << endl;
}
cout << endl;
}

int main(int argc, char* argv[])
{
if (argc < 3)
{
cout << "Usage: nqueens [Board Size] [Number of Solutions]" << endl;
return 1;
}

int board_size = atoi(argv[1]);
int solution_count = atoi(argv[2]);
vector<int> state;
state.resize(board_size);

Board *my_board = new Board(board_size);
int column = 0;
while (solution_count-- > 0 && my_board->backtrack(state, column, board_size))
print_solutions(my_board, state, board_size);

return 0;
}
  • 已修复:编译错误:cout 未知 -> #include iostream
  • 添加:print_solutions 中的额外换行符分离多个解决方案
  • 修复:print_solutions打印转置表
  • 修复:编译错误:print_solutions不返回值 -> void
  • 修复:argc检查
  • 已添加:solution_count搬家支持column调用网站
  • 修复:回溯代码重复(column--row = 0)
  • 修复:不必要的内部循环(row < board_size)
  • 未修复:my_board被泄露了
  • 不固定:整个Board类及其实例是不必要的

关于c++ - N 皇后解决方案 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20024429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com