• Skip to main content
  • Skip to primary sidebar

Phelela

  • Phelela
  • Build with me
  • Engineering Notes
  • Beyond Code
  • My GitHub

Backtracking in Action – Think Like an Engineer

16. April 2025

Welcome to the world of Sudoku and backtracking.

When I first started building a Sudoku game, I didn’t know what to expect. Solving Sudoku puzzles is fun – there are a few rules, nine numbers, and the challenge of filling out a 9×9 grid.
But writing a program that can generate a valid Sudoku grid? That’s a whole different level.

In my first version of the game, I used a simple algorithm: I placed numbers one by one – starting with 1, then 2, and so on – trying to fill every row, column, and 3×3 box.
It worked… sort of. But as a Sudoku fan, I knew there had to be a better, more elegant way.

That’s when I discovered backtracking.

Backtracking: Try Everything (and Then Some)

Backtracking is an algorithmic technique that tries everything – until it finds a valid solution.
When it hits a dead end, it simply goes back (tracks back) and tries a different path.

Let’s say you’re solving a Sudoku puzzle. You find an empty cell – what do you do?
Try 1. Doesn’t work? Try 2. Still doesn’t work? Try 3… and so on, up to 9.
If none of the values work, backtrack to the previous cell and try a different value there.

This process continues until the puzzle is solved – or until you’ve tried every possible option.

Recursion: A Problem That Solves Itself

Backtracking often uses recursion – when a function calls itself.
In my Sudoku generator, each call processes a row and a column in the grid.
If the function encounters an invalid state, it returns to the previous step and tries a new number (like resetting the cell: sudokuGrid[row][column] = 0).

Recursion helps break the problem down into smaller, more manageable parts – each solving a tiny piece of the puzzle.

Step by Step – and Try Again

Imagine trying to solve a Sudoku puzzle programmatically. You begin with number 1… it doesn’t fit.
Okay, try 2. Still doesn’t work? Onward to 3… and so on.

Check out the function isSudokuGridValid. It validates each step.
If it fails, the algorithm backtracks and starts again with a new number.

Want a real-life example?
When I renovated my home office, I placed the desk in a “perfect” spot… only to find out it didn’t work at all.
So I moved it. Then again. And again. Until finally, it clicked.

Backtracking, right there in action.

Conclusion: Trust the Process

Whenever you face a problem that feels impossible to solve, remember: backtracking is your friend.
Try, evaluate, step back, and try again.
Sometimes, the right answer shows up on your fifth attempt – and that’s totally okay.

in Build with me

Reader Interactions

you may also like
Why I Rebuilt My Sudoku App: A Developer’s Refactoring Journey
How I Handle Puzzle Solving and Validation: Generating a Sudoku Grid in Kotlin
Mastering the Development Cycle: A Guide to Writing Better Code
How to Write a Code Review Without Losing Your Mind (or Patience)

Trackbacks

  1. How to Generate and Validate a Sudoku Puzzle in Kotlin (With Backtracking Algorithm) says:
    24. June 2025 at 16:23

    […] a classic technique for solving constraint satisfaction problems. I discussed backtracking in my previous article, but today I’ll add some code […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Katarína Kováčová

Hi, I’m Katarina. I’m a mobile developer working with Android and iOS, and this blog is where I share things I learn, test, or find interesting in the world of mobile development.

I enjoy clear explanations, practical insights, and topics that make me think — a mix I try to bring into my writing as well.

Outside of tech, I like Sudoku, running, and reading thrillers and detective stories. I also cook and bake gluten-free, because I’m celiac, and experimenting in the kitchen has become part of my routine.

Welcome — hope you find something here that’s useful or inspiring.

My life in pictures

My kind of offline mode.
Old but still stealing the spotlight.
Chaos of ideas, order of pillows.
Where the world goes quiet for a moment.
Because every good day starts with a cookie.
2025 © Phelela
theme by soleilflare