Search code examples
algorithmdelphidelphi-7sudoku

Can't make/configure the difficult function


Sudoku ex1

While solving sudoku I can remove possibility digits (1) and (2) from the cells D[1,2] and D[2,2]. Because (8) and (9) are possible only in those cells, so those cells are either (8 and 9) or (9 and 8). This means that digits (1) and (2) are at the 3rd line of the D block. Thats why I can eliminate the possibility of the digit (1) from the cell A[3,3].

I have been configuring a function to do this during last 40 hours, but couldn't manage. Is there anyone who can make the function to detect this type of intellectual issue (eliminating some possibilities because some other n count of possibilities can exist only in n count of cells, in our case 2 digits 8 and 9 can exist in 2 cells D[1,2] and D[2,2]).

Please dont advice me about other functions of sudoku; I have already done them, the only algorithm that I couldn't program is this one. Btw you can use r[i] (string which consists the possibilities for the row number i), c[i] for the column, and b[i] for the blocks (ex: b[4] (in this image block A) = 1,2,3,4,5,6,7 because 8 and 9 are already defined). Thanks


Solution

  • I really don't see the problem, you basically already answered your problem. In general you should do the following:

    Step 1: Loop over all 9 cells of one block and check if (1) is contained only in two cells.

    Step 2: If not, try the next number. If yes, Loop over all 9 cells and check if (2) is also in those two cells but not in any other of the remaining 7.

    Step 3: If not, check the next number. If yes, remove the other possibilities of the two cells except for the two numbers you found and you are basically done.

    Step 4: If no matching number could be found for (1) (or any larger number that was chosen in the "not" part of step 2), start over from step 1 but trying the next number, unless you are already at 8, then you can stop.

    In the end you could dynamically extend the same pattern for 3 numbers in 3 cells, 4 numbers...