23 |
#include "solutioncontainer.h" |
#include "solutioncontainer.h" |
24 |
|
|
25 |
|
|
26 |
Queens::Queens(GUIUpdate *par, SolutionContainer *sol, int set_size, bool set_debug) : QThread(), solution(set_size) |
Queens::Queens(GUIUpdate *par, Solution* sol, SolutionContainer *container, int set_size, bool set_debug) : QThread() |
27 |
{ |
{ |
28 |
parent = par; |
m_parent = par; |
29 |
size = set_size; |
m_size = set_size; |
30 |
solution.setSize(size); |
m_solution = sol; |
31 |
debug = set_debug; |
m_solution->setSize(m_size); |
32 |
solutions = sol; |
m_debug = set_debug; |
33 |
|
m_solutions = container; |
34 |
} |
} |
35 |
|
|
36 |
bool Queens::solve() |
bool Queens::solve() |
45 |
|
|
46 |
bool Queens::solve(int row, bool findAll) |
bool Queens::solve(int row, bool findAll) |
47 |
{ |
{ |
48 |
if (blocked == true) |
if (m_blocked == true) |
49 |
return false; |
return false; |
50 |
for (int col=0; col<size; col++) { |
for (int col=0; col<m_size; col++) { |
51 |
if (checkCols(row,col) == true && |
if (checkCols(row,col) == true && |
52 |
checkCross1(row,col) == true && |
checkCross1(row,col) == true && |
53 |
checkCross2(row,col) == true) { |
checkCross2(row,col) == true) { |
54 |
|
|
|
solution.matrix[row][col] = true; |
|
55 |
|
|
56 |
if (row == (size-1) ) { |
m_solution->setMatrix(row,col,true); |
57 |
|
|
58 |
|
if (row == (m_size-1) ) { |
59 |
if (!findAll) |
if (!findAll) |
60 |
return true; |
return true; |
61 |
else { |
else { |
62 |
solutions->addSolution( solution ); |
m_solutions->addSolution( m_solution->copy() ); |
63 |
parent->foundSolution(); |
m_parent->foundSolution(); |
64 |
} |
} |
65 |
} |
} |
66 |
|
|
67 |
if ( solve(row+1,findAll) ) |
if ( solve(row+1,findAll) ) |
68 |
return true; |
return true; |
69 |
|
|
70 |
solution.matrix[row][col] = false; |
m_solution->setMatrix(row,col, false); |
71 |
} |
} |
72 |
} |
} |
73 |
return false; |
return false; |
76 |
|
|
77 |
bool Queens::checkRows(int row, int col) |
bool Queens::checkRows(int row, int col) |
78 |
{ |
{ |
79 |
for (int c=0; c<size; c++) { |
for (int c=0; c<m_size; c++) { |
80 |
if (solution.matrix[row][c] == true) |
if (m_solution->getMatrix(row,c) == true) |
81 |
return false; |
return false; |
82 |
} |
} |
83 |
|
|
86 |
|
|
87 |
bool Queens::checkCols(int row, int col) |
bool Queens::checkCols(int row, int col) |
88 |
{ |
{ |
89 |
for (int r=0; r<size; r++) { |
for (int r=0; r<m_size; r++) { |
90 |
if (solution.matrix[r][col] == true) |
if (m_solution->getMatrix(r,col) == true) |
91 |
return false; |
return false; |
92 |
} |
} |
93 |
return true; |
return true; |
97 |
{ |
{ |
98 |
int r=row; |
int r=row; |
99 |
int c=col; |
int c=col; |
100 |
for (int i=0; i<size; i++) { |
for (int i=0; i<m_size; i++) { |
101 |
if (solution.matrix[r][c] == true) |
if (m_solution->getMatrix(r,c) == true) |
102 |
return false; |
return false; |
103 |
r++; |
r++; |
104 |
c++; |
c++; |
105 |
if ( r>(size-1) || c>(size-1) ) |
if ( r>(m_size-1) || c>(m_size-1) ) |
106 |
break; |
break; |
107 |
} |
} |
108 |
r=row; |
r=row; |
109 |
c=col; |
c=col; |
110 |
for (int i=0; i<size;i++) { |
for (int i=0; i<m_size;i++) { |
111 |
if (solution.matrix[r][c] == true) |
if (m_solution->getMatrix(r,c) == true) |
112 |
return false; |
return false; |
113 |
r--; |
r--; |
114 |
c--; |
c--; |
122 |
{ |
{ |
123 |
int r=row; |
int r=row; |
124 |
int c=col; |
int c=col; |
125 |
for (int i=0; i<size; i++) { |
for (int i=0; i<m_size; i++) { |
126 |
if (solution.matrix[r][c] == true) |
if (m_solution->getMatrix(r,c) == true) |
127 |
return false; |
return false; |
128 |
r++; |
r++; |
129 |
c--; |
c--; |
130 |
if ( r>(size-1) || c<0 ) |
if ( r>(m_size-1) || c<0 ) |
131 |
break; |
break; |
132 |
} |
} |
133 |
r=row; |
r=row; |
134 |
c=col; |
c=col; |
135 |
for (int i=0; i<size; i++) { |
for (int i=0; i<m_size; i++) { |
136 |
if (solution.matrix[r][c] == true) |
if (m_solution->getMatrix(r,c) == true) |
137 |
return false; |
return false; |
138 |
r--; |
r--; |
139 |
c++; |
c++; |
140 |
if (r<0 || c>(size-1)) |
if (r<0 || c>(m_size-1)) |
141 |
break; |
break; |
142 |
} |
} |
143 |
return true; |
return true; |
145 |
|
|
146 |
void Queens::run() |
void Queens::run() |
147 |
{ |
{ |
148 |
blocked = false; |
m_blocked = false; |
149 |
findAll(); |
findAll(); |
150 |
|
|
151 |
if (blocked == false) |
if (m_blocked == false) |
152 |
parent->finishedSearch(); |
m_parent->finishedSearch(); |
153 |
} |
} |
154 |
|
|
155 |
void Queens::stop() |
void Queens::stop() |
156 |
{ |
{ |
157 |
blocked = true; |
m_blocked = true; |
158 |
} |
} |
159 |
|
|