I am in shock...
I add the MVC heuristic functor...
// Copyright 2007 Little Me
// This file is part of Littlemeuko.
//
// Littlemeuko 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.
//
// Littlemeuko 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 Littlemeuko; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "csp_sudoku.h"
void SelectNaiveUnassignedperator()(worldstate & ws, int & row, int & col)
{
ws.SelectUnassigned(row,col);
}
void SelectMCVUnassignedperator()(worldstate & ws, int & row, int & col)
{
int count = 0;
string placeholder;
for (int posvalue = 1; posvalue<10; posvalue++) {// number of unassigned positions area
for (int i = 0; i<9; i++) {
//find most constrained row
placeholder = ws.GetRow(i);
for (int j = 0; j<placeholder.size();j++) {
if (( placeholder[j] - '0' == 0) count++;
}
if (count == posvalue) { // yay!
for (int j = 0; j<placeholder.size();j++) {
if (( placeholder[j] - '0' == 0 )
{ row=i;col=j; goto Done; } // Goto considered harmful
}
}
else
count = 0;
}
}
Done:
;
};
I then change the one line of my main code from
if (solution = recursebacktrack(&neo,SelectNaiveUnassigned())) { ..
to
if (solution = recursebacktrack(&neo,SelectMCVUnassigned())) {
And the search for my previously pathological case (that took over 12 hours to solve) got solved in..
FIVE SECONDS.
I am giddy.
(And I only checked for the most constrained row - not column or region.. that would even make it faster... so soon to be added.)
Later:
I only moved the worst case to a different problem. But the search time is still reduced jusr not to 5 seconds.