build-a-sudoku-solver / lesson-14.md
Lesson 14 · Backtracking search

A complete, valid solution

A solver's answer is only trustworthy if it is both full and legal. Today you add the completeness check and use it with validity to confirm a solved grid really is a solution, closing the search chapter.

The goal

Report whether a grid is completely filled, and confirm a solved grid is both complete and valid.

Start here - the target
TO DO
Scenario: A solution is complete and valid
Giventhe grid parsed from "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
Whenthe solver runs and its result is checked
Thenthe result is complete (no blanks) and valid (no unit repeats a digit)
Andthe original puzzle is not complete, and the empty grid solved is also both complete and valid
Background

A grid is a real solution only when it is both complete - no blanks left - and valid - no unit repeats a digit. You already have validity; completeness is its easy partner, a scan for any remaining 0. Together they are the definition of “solved”, and checking them on the solver’s output is how you trust the answer rather than assuming the search got it right.

This pairing is also a quiet contract for everything ahead. The solver will only ever return a complete, valid grid or an honest failure; the uniqueness counter will lean on the same notion of a finished grid; and the generator will use validity and completeness to know when a puzzle has exactly one way to be filled. With a correct, fast, self-checking solver in hand, the next chapter makes it dramatically smarter - solving much of a puzzle by pure deduction before it ever needs to guess.

Make it work
// complete = every cell filled; a solution must be complete AND valid
func IsComplete(g [81]int) bool {
for i := 0; i < 81; i++ { if g[i] == 0 { return false } }
return true
}
// a true solution: IsComplete(sol) && IsValid(sol)
CheckpointDONE
You can confirm a solved grid is a genuine solution. The search chapter is complete; commit and stop here.