-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN_Queens.cpp
More file actions
201 lines (193 loc) · 4.14 KB
/
Copy pathN_Queens.cpp
File metadata and controls
201 lines (193 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
*N_Quenns
*2018年7月18日 21:57:58
*/
#include<iostream>
#include<stack>
#include<vector>
using std::stack;using std::vector;
struct Queen{
int x,y;
Queen(int xx, int yy){
x=xx;
y=yy;
}
bool operator ==(Queen const &q) const{ //重载相等运算符以此判定是否冲突
return (y==q.y)||(x+y==q.x+q.y)||(x-y==q.x-q.y); //同一列以及对角线;
}
bool operator !=(Queen const &q) const{return !(*this==q);}
};
template <typename T>
class Stack_1{ //利用vector实现了一个栈,可以进行find操作;
public:
vector<T> temp;
int find(T const& e){ //用来判定是否发生冲突
for(size_t i=0;i!=temp.size();++i) //不要使用temp.size()-1这种操作,易下标越界(size为0时)
{
if(e==temp[i])
return i;
}
return -1;
}
void push(T const& e){temp.push_back(e);}
T &top(){ return temp.back();}
void pop(){temp.pop_back();}
int size(){return temp.size();}
bool empty(){return temp.empty();}
};
int placeQueens(int N){
Stack_1<Queen> solu;
Queen q(0,0);
int nSolu=0;
do{//反复试探回溯
if(N<=solu.size()||N<=q.y) //若已出界或已形成一个全局解时
{
q=solu.top(); //回溯,并继续试探下一列
solu.pop();
q.y++;
}else
{
while((q.y<N)&&(!solu.empty())&&(0<=solu.find(q))) //比对已有皇后
{
q.y++; //发生冲突,移到下一列
}
if(N>q.y) //若存在可摆放的列
{
solu.push(q);
if(N<=solu.size()) //若部分解成为全局解,则通过全局变量计数
nSolu++;
q.x++; //移到下一行开头
q.y=0;
}
}
}while(0<q.x||q.y<N); //所有分支均已剪枝之后,算法结束
return nSolu;
}
int main(){
std::cout<<placeQueens(8);
return 0;
}
/*
/*
*N_Quenns
*2018年7月19日 15:49:41
*Input: 4
*Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
* ]
#include<iostream>
#include<stack>
#include<vector>
#include<string>
using std::stack;using std::vector; using std::string;using std::cout;
struct Queen{
int x,y;
Queen(int xx, int yy){
x=xx;
y=yy;
}
bool operator ==(Queen const &q) const{ //重载相等运算符以此判定是否冲突
return (y==q.y)||(x+y==q.x+q.y)||(x-y==q.x-q.y); //同一列以及对角线;
}
bool operator !=(Queen const &q) const{return !(*this==q);}
};
template <typename T>
class Stack_1{ //利用vector实现了一个栈,可以进行find操作;
public:
vector<T> temp;
int find(T const& e){ //用来判定是否发生冲突
for(size_t i=0;i!=temp.size();++i) //不要使用temp.size()-1这种操作,易下标越界(size为0时)
{
if(e==temp[i])
return i;
}
return -1;
}
void push(T const& e){temp.push_back(e);}
T &top(){ return temp.back();}
void pop(){temp.pop_back();}
int size(){return temp.size();}
bool empty(){return temp.empty();}
};
vector<vector<string> > placeQueens(int N){
Stack_1<Queen> solu;
Queen q(0,0);
Queen print_q(0,0);
int nSolu=0;
vector<vector<string> > pt; //存放最终结果
vector <string> s1,s1_copy,L,R,com; //定义一个向量及其副本,初始化向量
string s2;
char Q='Q';
char point='.';
for(int x=0;x<N;++x)
s2.push_back(point);
for(int y=0;y<N;++y)
{
s1.push_back(s2);
}
s1_copy=s1;
do{//反复试探回溯
if(N<=solu.size()||N<=q.y) //若已出界或已形成一个全局解时
{
q=solu.top(); //回溯,并继续试探下一列
solu.pop();
q.y++;
}else
{
while((q.y<N)&&(!solu.empty())&&(0<=solu.find(q))) //比对已有皇后
{
q.y++; //发生冲突,移到下一列
}
if(N>q.y) //若存在可摆放的列
{
solu.push(q);
if(N<=solu.size()) //若部分解成为全局解,则通过全局变量计数
{
// nSolu++;
for(size_t j=0;j!=solu.temp.size();++j)
{
print_q=solu.temp[j];
s1[print_q.x][print_q.y]=Q;
}
pt.push_back(s1);
s1=s1_copy;
}
q.x++; //移到下一行开头
q.y=0;
}
}
}while(0<q.x||q.y<N); //所有分支均已剪枝之后,算法结束
return pt;
// return nSolu;
}
int main(){
vector<vector<string> > pt=placeQueens(4);
for(vector<vector<string> >::iterator it_pt=pt.begin();it_pt!=pt.end();++it_pt) //输出结果
{
cout<<"[";
for(size_t i=0;i<(*it_pt).size();++i)
{
if(i==0)
{
cout<<"\"";
cout<<(*it_pt)[i];
cout<<"\"";
}else
{
cout<<","<<"\"";
cout<<(*it_pt)[i];
cout<<"\"";
}
}
cout<<"]";
}
return 0;
}
*/