-
Read Input File: Parse a grid of size
NxNfrom input files. Values may include:- Pre-filled numbers.
- Empty cells represented by a placeholder (e.g.,
-).
-
Initialize Data Structures:
- A 2D grid
Gridto represent the puzzle. - A
DomainGridto store sets of possible values for each cell. - Track
placedCells(pre-filled numbers and their coordinates). - A set
possibleNumbersrepresenting all numbers from1toN*N.
- A 2D grid
-
Domain Reduction:
- Use
updateDomainGridto calculate valid numbers for empty cells. - Place constraints based on Manhattan distances (vertical/horizontal) and diagonal distances.
- Use
-
Identify Missing Numbers:
- Analyze the
placedCellsand determine which numbers are missing.
- Analyze the
-
Propagate Constraints:
- For each missing number:
- Determine its closest pre-filled number.
- Update the domains for cells within valid distances.
- For each missing number:
-
Chains:
- Detect chains of consecutive numbers:
- If a number has only one valid position, place it.
- Optimize domains by removing invalid values from other cells.
- Detect chains of consecutive numbers:
-
Path Consistency:
- Ensure adjacent numbers (value
±1) can be placed:- Check both immediate neighbors and potential future placements.
- Ensure adjacent numbers (value
-
Recoverability:
- Verify that each placed number can connect to its successor and predecessor.
-
Scoring Criteria:
- Reward continuity (chains of consecutive numbers).
- Penalize isolated numbers.
- Reward cells with forced moves (single-option domains).
- Apply penalties for large domains (cells with many possibilities).
-
Purpose:
- Guide the solver to prioritize the most promising moves.
-
Branching:
- Find the cell with the smallest domain size (fewest possibilities).
- For each candidate value:
- Check path consistency before placing it.
- Evaluate the resulting grid using the scoring function.
-
Pruning:
- Use alpha-beta pruning to cut off unpromising branches:
- If a candidate's score is worse than
alpha, prune. - Update
alphawhen a better score is found.
- If a candidate's score is worse than
- Use alpha-beta pruning to cut off unpromising branches:
-
Backtracking:
- Recursively attempt to solve the grid.
- If no solution exists for the current branch, backtrack to explore alternatives.
-
Check for Completeness:
- The solver stops when all cells are filled with valid numbers (
1toN*N).
- The solver stops when all cells are filled with valid numbers (
-
Validate Solution:
- Compare the generated solution against the expected solution (if available in the input).
-
Output:
- Print the grid, execution time, and whether the solution is correct.
-
Constraint Satisfaction Problem (CSP):
- The problem is modeled as a grid where constraints enforce number placement.
-
Alpha-Beta Pruning:
- Optimizes the search space to avoid redundant computations.
-
Heuristic Evaluation:
- A scoring function guides the solver to prioritize promising moves.
-
Path Consistency:
- Ensures the solver maintains valid number placements at each step.
-
Backtracking with Domain Propagation:
- Incrementally explores possibilities while updating domains to maintain consistency.
- Parse input and initialize domains.
- Propagate constraints to reduce the search space.
- Use recursive alpha-beta search with heuristics to explore potential solutions.
- Verify the solution and print the results.