StandardSodukuSolution/sss.c
/**
 * Reduce possible values by confirmed units
 *
 * @param
 *      grid    Sudoku grid
 *
 * @return
 *      index of unit has least multiple possible values
 *      -1 on conflict or error
 *      C_UNIT on all units confirmed
 */
Reduce possible values by confirmed units
@param
grid    Sudoku grid

@return
index of unit has least multiple possible values
-1 on conflict or error
C_UNIT on all units confirmed
Reduce possible values by confirmed units @param grid Sudoku grid @return index of unit has least multiple possible values - 1 on conflict or error C_UNIT on all units confirmed
static int reducePossibleValues(Unit grid[]) {
    if (!grid) {
        return -1;
    }
    int i, j, found, c, minc, mini;
    while (1) {
        found = -1;
        mini = C_UNIT;
        minc = C_ROW + 1;
        for (i = 0; i < C_UNIT; ++i) {
            c = countPossibleValues(grid, i);
            if (c == 1) {
                grid[i].values = -(toReadableValue(grid, i));
                found = i;
                break;
            } else if (c > 1) {
                if (c < minc) {
                    mini = i;
                    minc = c;
                }
            }
        }
        if (found == -1) {
            return mini;
        }
        DBG("FND: %2d (%2d, %2d) = %d\n",
                found, ROW(found), COL(found), grid[found].values);
        for (i = ROW(found) * C_COL; i < (ROW(found) + 1) * C_COL; ++i) {
            if (i == found) {
                continue;
            }
            if (countPossibleValues(grid, i) > 0) {
                grid[i].values &= DEL(toReadableValue(grid, found));
                if (countPossibleValues(grid, i) == 0) {
                    return -1;
                }
            }
        }
        for (i = COL(found); i < C_ROW * C_COL; i += C_COL) {
            if (i == found) {
                continue;
            }
            if (countPossibleValues(grid, i) > 0) {
                grid[i].values &= DEL(toReadableValue(grid, found));
                if (countPossibleValues(grid, i) == 0) {
                    return -1;
                }
            }
        }
        for (i = BLK_HEAD(found); i < BLK_HEAD(found) + C_COL * C_BLK;
                i += C_COL) {
            for (j = 0; j < C_BLK; ++j) {
                if ((i + j) == found) {
                    continue;
                }
                if (countPossibleValues(grid, i + j) > 0) {
                    grid[i + j].values &= DEL(toReadableValue(grid, found));
                    if (countPossibleValues(grid, i + j) == 0) {
                        return -1;
                    }
                }
            }
        }
    }
    return -1;
}
static int reducePossibleValues ( Unit grid [ ] ) { if ( ! grid ) { return -1 ; } int i , j , found , c , minc , mini ; while ( 1 ) { found = -1 ; mini = C_UNIT ; minc = C_ROW + 1 ; for ( i = 0 ; i < C_UNIT ; ++ i ) { c = countPossibleValues ( grid , i ) ; if ( c == 1 ) { grid [ i ] . values = - ( toReadableValue ( grid , i ) ) ; found = i ; break ; } else if ( c > 1 ) { if ( c < minc ) { mini = i ; minc = c ; } } } if ( found == -1 ) { return mini ; } DBG ( " \n " , found , ROW ( found ) , COL ( found ) , grid [ found ] . values ) ; for ( i = ROW ( found ) * C_COL ; i < ( ROW ( found ) + 1 ) * C_COL ; ++ i ) { if ( i == found ) { continue ; } if ( countPossibleValues ( grid , i ) > 0 ) { grid [ i ] . values &= DEL ( toReadableValue ( grid , found ) ) ; if ( countPossibleValues ( grid , i ) == 0 ) { return -1 ; } } } for ( i = COL ( found ) ; i < C_ROW * C_COL ; i += C_COL ) { if ( i == found ) { continue ; } if ( countPossibleValues ( grid , i ) > 0 ) { grid [ i ] . values &= DEL ( toReadableValue ( grid , found ) ) ; if ( countPossibleValues ( grid , i ) == 0 ) { return -1 ; } } } for ( i = BLK_HEAD ( found ) ; i < BLK_HEAD ( found ) + C_COL * C_BLK ; i += C_COL ) { for ( j = 0 ; j < C_BLK ; ++ j ) { if ( ( i + j ) == found ) { continue ; } if ( countPossibleValues ( grid , i + j ) > 0 ) { grid [ i + j ] . values &= DEL ( toReadableValue ( grid , found ) ) ; if ( countPossibleValues ( grid , i + j ) == 0 ) { return -1 ; } } } } } return -1 ; }
Reduce possible values by confirmed units
 @param
      grid    Sudoku grid
Reduce possible values by confirmed units @param grid Sudoku grid
/* find a confirmed unit */ /* current row */ /* current column */ /* current block */