/[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 2 by torben, Thu Jul 19 22:26:42 2007 UTC revision 5 by torben, Fri Jul 20 01:22:53 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    
52                  if (checkCols(row,col) == true &&                  if (checkCols(row,col) == true &&
53                                  checkCross1(row,col) == true &&                                  checkCross1(row,col) == true &&
54                                  checkCross2(row,col) == true) {                                  checkCross2(row,col) == true)
55                    {
56    
                         solution.matrix[row][col] = true;  
57                                                    
58                          if (row == (size-1) ) {                          m_solution->setMatrix(row,col,true);
59                            
60                            if (row == (m_size-1) )
61                            {
62                                  if (!findAll)                                  if (!findAll)
63                                          return true;                                          return true;
64                                  else {                                  else
65                                          solutions->addSolution( solution.copy() );                                  {
66                                          parent->foundSolution();                                          m_solutions->addSolution( m_solution->copy() );
67                                            m_parent->foundSolution();
68                                  }                                  }
69                          }                          }
70    
71                          if ( solve(row+1,findAll) )                          if ( solve(row+1,findAll) )
72                                  return true;                                  return true;
73    
74                          solution.matrix[row][col] = false;                          m_solution->setMatrix(row,col, false);
75                  }                  }
76          }          }
77          return false;          return false;
# Line 74  bool Queens::solve(int row, bool findAll Line 80  bool Queens::solve(int row, bool findAll
80    
81  bool Queens::checkRows(int row, int col)  bool Queens::checkRows(int row, int col)
82  {  {
83          for (int c=0; c<size; c++) {          for (int c=0; c<m_size; c++) {
84                  if (solution.matrix[row][c] == true)                  if (m_solution->getMatrix(row,c) == true)
85                          return false;                          return false;
86          }          }
87                                    
# Line 84  bool Queens::checkRows(int row, int col) Line 90  bool Queens::checkRows(int row, int col)
90    
91  bool Queens::checkCols(int row, int col)  bool Queens::checkCols(int row, int col)
92  {  {
93          for (int r=0; r<size; r++) {          for (int r=0; r<m_size; r++) {
94                  if (solution.matrix[r][col] == true)                  if (m_solution->getMatrix(r,col) == true)
95                          return false;                          return false;
96          }          }
97          return true;          return true;
# Line 95  bool Queens::checkCross1(int row, int co Line 101  bool Queens::checkCross1(int row, int co
101  {  {
102          int r=row;          int r=row;
103          int c=col;          int c=col;
104          for (int i=0; i<size; i++) {          for (int i=0; i<m_size; i++) {
105                  if (solution.matrix[r][c] == true)                  if (m_solution->getMatrix(r,c) == true)
106                          return false;                          return false;
107                  r++;                  r++;
108                  c++;                  c++;
109                  if ( r>(size-1) || c>(size-1) )                  if ( r>(m_size-1) || c>(m_size-1) )
110                          break;                                    break;          
111          }          }
112          r=row;          r=row;
113          c=col;          c=col;
114          for (int i=0; i<size;i++) {          for (int i=0; i<m_size;i++) {
115                  if (solution.matrix[r][c] == true)                  if (m_solution->getMatrix(r,c) == true)
116                          return false;                          return false;
117                  r--;                  r--;
118                  c--;                  c--;
# Line 120  bool Queens::checkCross2(int row, int co Line 126  bool Queens::checkCross2(int row, int co
126  {  {
127          int r=row;          int r=row;
128          int c=col;          int c=col;
129          for (int i=0; i<size; i++) {          for (int i=0; i<m_size; i++) {
130                  if (solution.matrix[r][c] == true)                  if (m_solution->getMatrix(r,c) == true)
131                          return false;                          return false;
132                  r++;                  r++;
133                  c--;                  c--;
134                  if ( r>(size-1) || c<0 )                  if ( r>(m_size-1) || c<0 )
135                          break;                          break;
136          }          }
137          r=row;          r=row;
138          c=col;          c=col;
139          for (int i=0; i<size; i++) {          for (int i=0; i<m_size; i++) {
140                  if (solution.matrix[r][c] == true)                  if (m_solution->getMatrix(r,c) == true)
141                          return false;                          return false;
142                  r--;                  r--;
143                  c++;                  c++;
144                  if (r<0 || c>(size-1))                  if (r<0 || c>(m_size-1))
145                          break;                          break;
146          }          }
147          return true;          return true;
# Line 143  bool Queens::checkCross2(int row, int co Line 149  bool Queens::checkCross2(int row, int co
149    
150  void Queens::run()  void Queens::run()
151  {  {
152          blocked = false;          m_blocked = false;
153          findAll();          findAll();
154                    
155          if (blocked == false)          if (m_blocked == false)
156                  parent->finishedSearch();                  m_parent->finishedSearch();
157  }  }
158    
159  void Queens::stop()  void Queens::stop()
160  {  {
161          blocked = true;          m_blocked = true;
162  }  }
163    

Legend:
Removed from v.2  
changed lines
  Added in v.5

  ViewVC Help
Powered by ViewVC 1.1.20