Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
10 REPLIES 10

Sandra_Rossi
Active Contributor

0 Kudos

For example, I will do 3 x 3 sudoku and with the values I set fixed in my program, the program will scan each row and column and give me the output when it places the appropriate numbers. so there will be no user interaction.                                                   I am looking for such a simple code, where can I actually find it?

Sandra_Rossi
Active Contributor
0 Kudos

Sudoku solving algorithms - Backtracking (brute-force search) - Wikipedia:

"A brute force algorithm visits the empty cells in some order, filling in digits sequentially, or backtracking when the number is found to be not valid. Briefly, a program would solve a puzzle by placing the digit "1" in the first cell and checking if it is allowed to be there. If there are no violations (checking row, column, and box constraints) then the algorithm advances to the next cell and places a "1" in that cell. When checking for violations, if it is discovered that the "1" is not allowed, the value is advanced to "2". If a cell is discovered where none of the 9 digits is allowed, then the algorithm leaves that cell blank and moves back to the previous cell. The value in that cell is then incremented by one. This is repeated until the allowed value in the last (81st) cell is discovered."

Sandra_Rossi
Active Contributor

For information, for the fun, I could do the code from scratch in two hours by applying Sudoku solving algorithms - Backtracking (brute-force search) - Wikipedia), no need to convert C code.

"A brute force algorithm visits the empty cells in some order, filling in digits sequentially, or backtracking when the number is found to be not valid. Briefly, a program would solve a puzzle by placing the digit "1" in the first cell and checking if it is allowed to be there. If there are no violations (checking row, column, and box constraints) then the algorithm advances to the next cell and places a "1" in that cell. When checking for violations, if it is discovered that the "1" is not allowed, the value is advanced to "2". If a cell is discovered where none of the 9 digits is allowed, then the algorithm leaves that cell blank and moves back to the previous cell. The value in that cell is then incremented by one. This is repeated until the allowed value in the last (81st) cell is discovered."

To help you start and learn by yourself, here are the start and the end of the program. I didn't use any subroutine/method, just loops and conditions. It must be approximately 120 lines of code.

TYPES tv_row TYPE STANDARD TABLE OF i WITH EMPTY KEY.
TYPES tt_cell TYPE STANDARD TABLE OF tv_row WITH EMPTY KEY.

DATA(cells) = VALUE tt_cell(
    ( VALUE #( ( 5 ) ( 3 ) (   ) (   ) ( 7 ) (   ) (   ) (   ) (   ) ) )
    ( VALUE #( ( 6 ) (   ) (   ) ( 1 ) ( 9 ) ( 5 ) (   ) (   ) (   ) ) )
    ( VALUE #( (   ) ( 9 ) ( 8 ) (   ) (   ) (   ) (   ) ( 6 ) (   ) ) )
    ( VALUE #( ( 8 ) (   ) (   ) (   ) ( 6 ) (   ) (   ) (   ) ( 3 ) ) )
    ( VALUE #( ( 4 ) (   ) (   ) ( 8 ) (   ) ( 3 ) (   ) (   ) ( 1 ) ) )
    ( VALUE #( ( 7 ) (   ) (   ) (   ) ( 2 ) (   ) (   ) (   ) ( 6 ) ) )
    ( VALUE #( (   ) ( 6 ) (   ) (   ) (   ) (   ) ( 2 ) ( 8 ) (   ) ) )
    ( VALUE #( (   ) (   ) (   ) ( 4 ) ( 1 ) ( 9 ) (   ) (   ) ( 5 ) ) )
    ( VALUE #( (   ) (   ) (   ) (   ) ( 8 ) (   ) (   ) ( 7 ) ( 9 ) ) ) ).

DATA(backup_cells) = cells.

DATA(row_match) = 0.

DATA(row) = 1.
DATA(col) = 1.
DO.
  " 0 means that the digit is to be determined
  IF backup_cells[ row ][ col ] = 0.

    ...

  ENDIF.

  IF backup_cells[ row ][ col ] <> 0
      OR digit_determination_succeeded = abap_true.
    " go to the next cell
    IF col < 9.
      col = col + 1.
    ELSEIF row = 9.
      EXIT. " solved !
    ELSE.
      row = row + 1.
      col = 1.
    ENDIF.
  ENDIF.
ENDDO.

ASSERT cells = VALUE tt_cell(
    ( VALUE #( ( 5 ) ( 3 ) ( 4 ) ( 6 ) ( 7 ) ( 8 ) ( 9 ) ( 1 ) ( 2 ) ) )
    ( VALUE #( ( 6 ) ( 7 ) ( 2 ) ( 1 ) ( 9 ) ( 5 ) ( 3 ) ( 4 ) ( 8 ) ) )
    ( VALUE #( ( 1 ) ( 9 ) ( 8 ) ( 3 ) ( 4 ) ( 2 ) ( 5 ) ( 6 ) ( 7 ) ) )
    ( VALUE #( ( 8 ) ( 5 ) ( 9 ) ( 7 ) ( 6 ) ( 1 ) ( 4 ) ( 2 ) ( 3 ) ) )
    ( VALUE #( ( 4 ) ( 2 ) ( 6 ) ( 8 ) ( 5 ) ( 3 ) ( 7 ) ( 9 ) ( 1 ) ) )
    ( VALUE #( ( 7 ) ( 1 ) ( 3 ) ( 9 ) ( 2 ) ( 4 ) ( 8 ) ( 5 ) ( 6 ) ) )
    ( VALUE #( ( 9 ) ( 6 ) ( 1 ) ( 5 ) ( 3 ) ( 7 ) ( 2 ) ( 8 ) ( 4 ) ) )
    ( VALUE #( ( 2 ) ( 8 ) ( 7 ) ( 4 ) ( 1 ) ( 9 ) ( 6 ) ( 3 ) ( 5 ) ) )
    ( VALUE #( ( 3 ) ( 4 ) ( 5 ) ( 2 ) ( 8 ) ( 6 ) ( 1 ) ( 7 ) ( 9 ) ) ) ).