Судоку Отступление Недействительно Судоку
Я создал решатель судоку Backtracking, и он работает просто отлично, но теперь я хочу выдать ошибку, если судоку невозможно решить, потому что он недействителен, например, если дано это судоку:
http://img5.imageshack.us/img5/2241/sudokugq.jpg
Что я могу сделать, чтобы мой метод решения выдал ошибку, если она не разрешима? Я всегда в конечном итоге с нулями или застрял в цикле.
public void solve( int row, int col ) throws Exception
{
// Throw an exception to stop the process if the puzzle is solved
if( row > 8 ){
//Gelöst
}
// If the cell is not empty, continue with the next cell
if( sudo[row][col] != 0 ){
next( row, col ) ;
}
else
{
// Find a valid number for the empty cell
for( int num = 1; num < 10; num++ )
{
if( checkHorizontal(row,num) == false && checkVertikal(col,num)== false&& checkBox(row,col,num)== false )
{
sudo[row][col] = num ;
// Delegate work on the next cell to a resudosive call
next( row, col ) ;
}
}
// No valid number was found, clean up and return to caller
sudo[row][col] = 0 ;
}
}
//Ruft solve für das nächste Feld auf
public void next( int row, int col ) throws Exception
{
if( col < 8 )
solve( row, col + 1 ) ;
else
solve( row + 1, 0 ) ;
}