Dsa
Backtracking
Hard
Puzzle Solver
You are tasked to solve a simple numeric puzzle. You have a 2D grid of size n x n filled with numbers from 1 to n and 0 representing empty spaces. Your goal is to fill in the empty spaces such that each number from 1 to n appears exactly once in each row and each column. If no solution is possible, return "No solution".
Input Format:
- The first line contains the integer `n` (1 ≤ n ≤ 9).
- The next n lines each contain n integers where 0 represents an empty space.
Output Format:
- Print the filled grid row-wise if a solution exists. If no solution exists, print "No solution".
Example:
Input:
3
1 0 3
0 2 0
0 0 1
Output:
1 2 3
3 2 1
2 1 3
Key concepts
backtrackingconstraint satisfactionpuzzle
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now