/[projects]/queensgui/src/queens.cpp
ViewVC logotype

Diff of /queensgui/src/queens.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by torben, Thu Jul 19 22:26:42 2007 UTC revision 4 by torben, Thu Jul 19 23:44:01 2007 UTC
# Line 23  Line 23 
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()
# Line 44  void Queens::findAll() Line 45  void Queens::findAll()
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.copy() );                                          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;
# Line 74  bool Queens::solve(int row, bool findAll Line 76  bool Queens::solve(int row, bool findAll
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                                    
# Line 84  bool Queens::checkRows(int row, int col) Line 86  bool Queens::checkRows(int row, int col)
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;
# Line 95  bool Queens::checkCross1(int row, int co Line 97  bool Queens::checkCross1(int row, int co
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--;
# Line 120  bool Queens::checkCross2(int row, int co Line 122  bool Queens::checkCross2(int row, int co
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;
# Line 143  bool Queens::checkCross2(int row, int co Line 145  bool Queens::checkCross2(int row, int co
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    

Legend:
Removed from v.3  
changed lines
  Added in v.4

  ViewVC Help
Powered by ViewVC 1.1.20