27 |
#include <qstring.h> |
#include <qstring.h> |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
#include "queensmain.h" |
#include "queensmain.h" |
32 |
#include "board.h" |
#include "board.h" |
33 |
#include "queens.h" |
#include "queens.h" |
34 |
#include "solutionvector.h" |
|
35 |
#include "solutionlist.h" |
#include "solution.h" |
36 |
#include "solutionhash.h" |
#include "solutionmatrix.h" |
37 |
#include "solutionmnvector.h" |
#include "solutionint.h" |
38 |
|
|
39 |
|
#include "containervector.h" |
40 |
|
#include "containerlist.h" |
41 |
|
#include "containerhash.h" |
42 |
|
#include "containermnvector.h" |
43 |
|
#include "containerminimalvector.h" |
44 |
|
|
45 |
#include "config.h" |
#include "config.h" |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
QueensMain::QueensMain(QWidget *parent, const char *name) |
QueensMain::QueensMain(QWidget *parent, const char *name) |
50 |
: QDialog(parent, name) |
: QDialog(parent, name) |
51 |
{ |
{ |
83 |
m_sizeSelector->setValue( 8 ); |
m_sizeSelector->setValue( 8 ); |
84 |
m_board->setSize( 8 ); |
m_board->setSize( 8 ); |
85 |
|
|
86 |
|
m_storage = StorageInt; |
87 |
m_sortalgo = SortList; |
m_sortalgo = SortList; |
88 |
m_solutions = NULL; |
m_solutions = NULL; |
89 |
m_queens = NULL; |
m_queens = NULL; |
90 |
|
m_sol = NULL; |
91 |
|
|
92 |
connect(m_quit, |
connect(m_quit, |
93 |
SIGNAL( clicked() ), |
SIGNAL( clicked() ), |
130 |
QueensMain::~QueensMain() |
QueensMain::~QueensMain() |
131 |
{ |
{ |
132 |
delete m_board; |
delete m_board; |
133 |
|
|
134 |
|
if (m_solutions != NULL) |
135 |
|
delete m_solutions; |
136 |
|
if (m_sol != NULL) |
137 |
|
delete m_sol; |
138 |
} |
} |
139 |
|
|
140 |
void QueensMain::start() |
void QueensMain::start() |
145 |
m_list->clear(); |
m_list->clear(); |
146 |
m_status->setText( QString("Searching ...") ); |
m_status->setText( QString("Searching ...") ); |
147 |
|
|
148 |
|
m_board->setMatrix(0); |
149 |
|
|
150 |
if (m_queens != NULL) { |
if (m_queens != NULL) { |
151 |
m_queens->wait(); |
m_queens->wait(); |
152 |
delete m_queens; |
delete m_queens; |
153 |
|
m_queens = 0; |
154 |
} |
} |
155 |
|
|
156 |
if (m_solutions != NULL) |
if (m_solutions != NULL) |
157 |
delete m_solutions; |
delete m_solutions; |
158 |
switch (m_sortalgo) { |
switch (m_sortalgo) { |
159 |
case SortList: |
case SortList: |
160 |
m_solutions = new SolutionList(this); |
m_solutions = new ContainerList(this); |
161 |
break; |
break; |
162 |
case SortVector: |
case SortVector: |
163 |
m_solutions = new SolutionVector(this); |
m_solutions = new ContainerVector(this); |
164 |
break; |
break; |
165 |
case SortHash: |
case SortHash: |
166 |
m_solutions = new SolutionHash(this); |
m_solutions = new ContainerHash(this); |
167 |
break; |
break; |
168 |
case SortMNVector: |
case SortMNVector: |
169 |
m_solutions = new SolutionMNVector(this); |
m_solutions = new ContainerMNVector(this); |
170 |
break; |
break; |
171 |
|
case SortMinimalVector: |
172 |
|
m_solutions = new ContainerMinimalVector(this); |
173 |
|
break; |
174 |
} |
} |
175 |
|
|
176 |
|
if (m_sol != NULL) |
177 |
|
delete m_sol; |
178 |
|
|
179 |
|
switch(this->m_storage) { |
180 |
|
case StorageInt: |
181 |
|
m_sol = new SolutionInt(m_sizeSelector->value()); |
182 |
|
break; |
183 |
|
case StorageMatrix: |
184 |
|
m_sol = new SolutionMatrix(m_sizeSelector->value()); |
185 |
|
break; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
m_elapsed.start(); |
m_elapsed.start(); |
190 |
m_time.start(); |
m_time.start(); |
191 |
m_queens = new Queens(this, m_solutions, m_sizeSelector->value() ,false); |
m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false); |
192 |
m_queens->start(); |
m_queens->start(); |
193 |
} |
} |
194 |
|
|
235 |
void QueensMain::showSolution() |
void QueensMain::showSolution() |
236 |
{ |
{ |
237 |
int index = m_list->currentItem(); |
int index = m_list->currentItem(); |
238 |
Solution sol = m_solutions->solution( index ); |
Solution* sol = m_solutions->solution( index ); |
239 |
m_board->setMatrix( sol ); |
m_board->setMatrix( sol ); |
240 |
} |
} |
241 |
|
|
266 |
|
|
267 |
void QueensMain::uniqueSolutions() |
void QueensMain::uniqueSolutions() |
268 |
{ |
{ |
269 |
m_totalcount = m_solutions->numSolutions(); |
m_totalcount = m_solutions->totalSolutions(); |
270 |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
271 |
|
|
272 |
m_elapsed.restart(); |
m_elapsed.restart(); |
289 |
{ |
{ |
290 |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
291 |
contextMenu->setCheckable( true ); |
contextMenu->setCheckable( true ); |
292 |
QLabel *caption = new QLabel("<b><i>Sort algorithm</i></b>", this); |
QLabel *sortCaption = new QLabel("<b><i>Container class</i></b>", this); |
293 |
caption->setAlignment( Qt::AlignCenter ); |
sortCaption->setAlignment( Qt::AlignCenter ); |
294 |
contextMenu->insertItem( caption ); |
contextMenu->insertItem( sortCaption ); |
295 |
|
|
296 |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
297 |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
298 |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
299 |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
300 |
|
contextMenu->insertItem("Minimal Vector", this, SLOT(sortMinimalVector()), 0, 5); |
301 |
|
|
302 |
|
QLabel *storageCaption = new QLabel("<b><i>Solution class</i></b>", this); |
303 |
|
storageCaption->setAlignment( Qt::AlignCenter ); |
304 |
|
contextMenu->insertItem( storageCaption ); |
305 |
|
contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 6); |
306 |
|
contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 7); |
307 |
|
|
308 |
|
|
309 |
switch (m_sortalgo) { |
switch (m_sortalgo) { |
310 |
case SortList: |
case SortList: |
319 |
case SortMNVector: |
case SortMNVector: |
320 |
contextMenu->setItemChecked( 4, true); |
contextMenu->setItemChecked( 4, true); |
321 |
break; |
break; |
322 |
|
case SortMinimalVector: |
323 |
|
contextMenu->setItemChecked( 5, true); |
324 |
|
break; |
325 |
} |
} |
326 |
|
|
327 |
|
switch (m_storage) { |
328 |
|
case StorageMatrix: |
329 |
|
contextMenu->setItemChecked(6, true); |
330 |
|
break; |
331 |
|
case StorageInt: |
332 |
|
contextMenu->setItemChecked(7, true); |
333 |
|
break; |
334 |
|
} |
335 |
|
|
336 |
|
|
337 |
contextMenu->exec( event->globalPos() ); |
contextMenu->exec( event->globalPos() ); |
338 |
delete contextMenu; |
delete contextMenu; |
339 |
} |
} |
370 |
m_sortalgo = SortMNVector; |
m_sortalgo = SortMNVector; |
371 |
} |
} |
372 |
|
|
373 |
|
void QueensMain::sortMinimalVector() |
374 |
|
{ |
375 |
|
m_sortalgo = SortMinimalVector; |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
void QueensMain::storageInt() |
380 |
|
{ |
381 |
|
m_storage = StorageInt; |
382 |
|
} |
383 |
|
|
384 |
|
void QueensMain::storageMatrix() |
385 |
|
{ |
386 |
|
m_storage = StorageMatrix; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
|