结果
341步
1--1
2--3
3--1
4--2
5--1
6--5
7--1
8--2
9--1
10--3
11--1
12--2
13--1
14--4
15--1
16--2
17--1
18--3
19--1
20--2
21--1
22--7
23--1
24--2
25--1
26--3
27--1
28--2
29--1
30--4
31--1
32--2
33--1
34--3
35--1
36--2
37--1
38--5
39--1
40--2
41--1
42--3
43--1
44--2
45--1
46--4
47--1
48--2
49--1
50--3
51--1
52--2
53--1
54--6
55--1
56--2
57--1
58--3
59--1
60--2
61--1
62--4
63--1
64--2
65--1
66--3
67--1
68--2
69--1
70--5
71--1
72--2
73--1
74--3
75--1
76--2
77--1
78--4
79--1
80--2
81--1
82--3
83--1
84--2
85--1
86--9
87--1
88--2
89--1
90--3
91--1
92--2
93--1
94--4
95--1
96--2
97--1
98--3
99--1
100--2
101--1
102--5
103--1
104--2
105--1
106--3
107--1
108--2
109--1
110--4
111--1
112--2
113--1
114--3
115--1
116--2
117--1
118--6
119--1
120--2
121--1
122--3
123--1
124--2
125--1
126--4
127--1
128--2
129--1
130--3
131--1
132--2
133--1
134--5
135--1
136--2
137--1
138--3
139--1
140--2
141--1
142--4
143--1
144--2
145--1
146--3
147--1
148--2
149--1
150--7
151--1
152--2
153--1
154--3
155--1
156--2
157--1
158--4
159--1
160--2
161--1
162--3
163--1
164--2
165--1
166--5
167--1
168--2
169--1
170--3
171--1
172--2
173--1
174--4
175--1
176--2
177--1
178--3
179--1
180--2
181--1
182--6
183--1
184--2
185--1
186--3
187--1
188--2
189--1
190--4
191--1
192--2
193--1
194--3
195--1
196--2
197--1
198--5
199--1
200--2
201--1
202--3
203--1
204--2
205--1
206--4
207--1
208--2
209--1
210--3
211--1
212--2
213--1
214--8
215--1
216--2
217--1
218--3
219--1
220--2
221--1
222--4
223--1
224--2
225--1
226--3
227--1
228--2
229--1
230--5
231--1
232--2
233--1
234--3
235--1
236--2
237--1
238--4
239--1
240--2
241--1
242--3
243--1
244--2
245--1
246--6
247--1
248--2
249--1
250--3
251--1
252--2
253--1
254--4
255--1
256--2
257--1
258--3
259--1
260--2
261--1
262--5
263--1
264--2
265--1
266--3
267--1
268--2
269--1
270--4
271--1
272--2
273--1
274--3
275--1
276--2
277--1
278--7
279--1
280--2
281--1
282--3
283--1
284--2
285--1
286--4
287--1
288--2
289--1
290--3
291--1
292--2
293--1
294--5
295--1
296--2
297--1
298--3
299--1
300--2
301--1
302--4
303--1
304--2
305--1
306--3
307--1
308--2
309--1
310--6
311--1
312--2
313--1
314--3
315--1
316--2
317--1
318--4
319--1
320--2
321--1
322--3
323--1
324--2
325--1
326--5
327--1
328--2
329--1
330--3
331--1
332--2
333--1
334--4
335--1
336--2
337--1
338--3
339--1
340--2
341--1
源代码
#include<bits/stdc++.h>
using namespace std;
class step{
public:
vector<int> v;
vector<int> r;
step(){
this->v.clear();
for(int i=0;i<9;i++){
v.push_back(1);
}
this->r.clear();
}
step(const step &s){
this->v=s.v;
this->r=s.r;
}
int key(){
int sum=0;
for(int i=0;i<v.size();i++){
sum=sum*2+v[i];
}
return sum;
}
bool can(int i){
if(i==0){
return true;
}
if(v[i-1]==0){
return false;
}
for(int j=0;j<i-1;j++){
if(v[j]==1){
return false;
}
}
return true;
}
void change(int i){
this->v[i]=1-this->v[i];
this->r.push_back(i+1);
}
bool success(){
for(int i=0;i<this->v.size();i++){
if(this->v[i]==1){
return false;
}
}
return true;
}
void display(){
cout<<this->r.size()<<"步"<<endl;
cout<<endl;
for(int i=0;i<this->r.size();i++){
printf("%3d--%d\n",i+1,this->r[i]);
}
}
};
int main(){
step first;
step now;
step next;
queue<step> q;
q.push(first);
map<int,step> m;
m.insert(pair<int,step>(first.key(),first));
while(q.size()>0){
now=q.front();
q.pop();
if(now.success()){
now.display();
break;
}
for(int i=0;i<now.v.size();i++){
if(now.can(i)){
next=now;
next.change(i);
if(m.find(next.key())==m.end()){
q.push(next);
m.insert(pair<int,step>(next.key(),next));
}
}
}
}
return 0;
}
注意事项
1、“最少”问题我们一般都会将算法定位到广度优先搜索中
2、广搜需要注意给自定义数据结构设置好比较容易计算和比较的键来排重
3、我们在写之前要明确节点要拥有的属性和方法: