haechandev
Algorithms

[Baekjoon 2580] Sudoku

Fill empty cells with backtracking while tracking numbers used in each row, column, and 3×3 box.

September 1, 2022

For every empty cell, try numbers one through nine and proceed to the next cell when the rules are satisfied. I used three kinds of arrays to avoid rescanning rows and columns each time.

  • row[y][number]
  • column[x][number]
  • square[(y / 3) * 3 + x / 3][number]

When placing a number, mark all three arrays. If recursion fails, clear the marks again. Once a complete board is found, print it and stop.

Problem: Baekjoon · Code: View on GitHub