N皇后(遞迴經典演算法)
阿新 • • 發佈:2019-02-18
一、N皇后
1、題目
將n個皇后擺放在N*N的棋盤中,互相不可攻擊,有多少種擺放方式,每種擺放方式具體是怎樣的?
2、解題思路
解題思路:
1、將棋盤放在一個二維陣列中,同時設定方向陣列:
static const int dx[] = {-1,1,0,0,-1,-1,1,1,};
static const int dy[] = {-1,0,-1,1,-1,1,-1,1};
分別按照方向陣列的8個方向分別延伸N個問題,只要不超過邊界,mark[][] = 1;其餘為0;
2、對於N*N的棋盤,每行都要放置1個且只能放置1個皇后,利用遞迴對棋盤的每一行放置皇后,放置時,按列順序尋找可以放置皇后的列,若可以放置皇后,將皇后放置該位置,並更新mark標記陣列,遞迴進行下一行皇后放置,當該次遞迴結束後,恢復mark陣列,並嘗試下一個可能放皇后的列;當遞迴可以完成N列的N個皇后放置,則將該結果儲存並返回。
3、棋盤的設定與皇后的放置
4、N皇后回溯演算法
5、程式實現
#include<iostream>
#include<vector>
using namespace std;
void put_down_the_queue(int x, int y,
std::vector<std::vector<int>>& mark)
{
static const int dx[] = {-1,1,0,0,-1,-1,1,1};
static const int dy[]= {0,0,-1,1,-1,1 ,-1,1};
mark[x][y] = 1;
for(int i = 1;i< mark.size();i++)
{
for(int j = 0; j < 8; j++)
{
int new_x = x + i*dx[j];//*********************
int new_y = y + i*dy[j];//**********************
if( (new_x>=0 && new_x<mark.size()) && (new_y >=0 &&new_y<mark.size()) )//注意>=********************
{
mark[new_x][new_y] = 1;
}
}
}
}
class solution
{
public:
std::vector<std::vector<std::string>> solveNQuess(int n)
{
std::vector<std::vector<std::string>> result;
std::vector<std::vector<int>> mark;
std::vector<std::string> location;
for(int i = 0;i<n;i++)
{
mark.push_back(std::vector<int>());
for(int j = 0; j <n;j++)
{
mark[i].push_back(0);
}
location.push_back("");
location[i].append(n,'.');
}
generate(0,n,location,result,mark);
return result;
}
private:
void generate(int k, int n,
std::vector<std::string>&location,
std::vector<std::vector<std::string>>&result,
std::vector<std::vector<int>> &mark
)
{
if(k == n)
{
result.push_back(location);
return ;
}
for(int i = 0; i<n; i++)
{
if(mark[k][i] == 0)
{
std::vector<std::vector<int>> tmp_mark = mark;
location[k][i] = 'Q';
put_down_the_queue(k,i,mark);
generate(k+1, n, location,result,mark);
mark = tmp_mark;
location[k][i] = '.';
}
}
}
};
int main()
{
std::vector<std::vector<std::string>> result;
solution solve;
result = solve.solveNQuess (4);
for(int i = 0; i<result.size();i++)
{
printf("i = %d\n",i);
for(int j = 0; j<result[i].size();j++)
{
printf("%s\n",result[i][j].c_str() );
}
printf("\n");
}
return 0;
}
6、結果展示