/*************************************************************************** * Copyright (C) 2005 by Torben Nielsen * * torben@t-hoerup.dk * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "solution.h" #include "queens.h" #include "solutioncontainer.h" Queens::Queens(GUIUpdate *par, SolutionContainer *sol, int set_size, bool set_debug) : QThread(), solution(set_size) { parent = par; size = set_size; solution.setSize(size); debug = set_debug; solutions = sol; } bool Queens::solve() { return solve(0, false); } void Queens::findAll() { solve(0,true); } bool Queens::solve(int row, bool findAll) { if (blocked == true) return false; for (int col=0; coladdSolution( solution.copy() ); parent->foundSolution(); } } if ( solve(row+1,findAll) ) return true; solution.matrix[row][col] = false; } } return false; } bool Queens::checkRows(int row, int col) { for (int c=0; c(size-1) || c>(size-1) ) break; } r=row; c=col; for (int i=0; i(size-1) || c<0 ) break; } r=row; c=col; for (int i=0; i(size-1)) break; } return true; } void Queens::run() { blocked = false; findAll(); if (blocked == false) parent->finishedSearch(); } void Queens::stop() { blocked = true; }