实验目的:
1. 了解皇后相互攻击的条件:如果任意两个皇后在同一行,同一列或同一对 角线,则她们相互攻击。
2. 运用迭代的方法实现 6 皇后问题,求解得到皇后不相互攻击的一个解。
3. 在运用迭代的方法实现编程时,要注意回溯点。
实验内容:
对 6 皇后问题求解,用数组 c[1 … 6] 来存皇后的位置。 c[i]=j 表示第 i 个皇后放在第 j 列。 最后程序运行的结果是 c[1 … 6]={1,5,8,6,3,7 }
算法思路:
我们不再用数组来存储位置,而是用一个整数k,k一开始等于0. 不是普通的0.我们也不比较了,直接用两个整数l和r 记录在斜线在当前行不能走的位置。如果是n皇后, 那么用一个整数
nn = 1 << n 表示结束。
举个例子: 8皇后问题。
初始化 那么我们就变成二进制的角度来看这些初始化的数据吧。
k = 00000000, l = 00000000, r = 00000000; nn = 11111111; (<- 8个0 8个1)
k: 每个位置i的0表示没有皇后,1表示在第i个位置放了一个皇后。
l: 0表示之前所有的列中放的皇后斜率为-1的线上没有涉及这个位置, 1 表示涉及到了,不能放皇后
r: 同l, 所有斜率为1的线涉及的位置。
l和r的实现:
比如k = 00110001. 我要在第4个为位置放一个皇后, 假设l和r都没有涉及这个位置。
那么这个位置x= 00001000.
k = (k & x) = 00111001.
l = (l & x) << 1.
r = (r & x) >> 1.
假设l = 00110001, r = 00100010.下一行,l表示斜率为-1不能放的位置, 那么第i+1行 l 中所有为1的数字都需要向左移动一位,r需要向右移动一位。 l & x 也就是加上当前选中的位置一起移动。
程序源码:
#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;
}
实验结果: