Skip to content

SteinerHannes/Hidoku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Step 1: Input Parsing

  1. Read Input File: Parse a grid of size NxN from input files. Values may include:

    • Pre-filled numbers.
    • Empty cells represented by a placeholder (e.g., -).
  2. Initialize Data Structures:

    • A 2D grid Grid to represent the puzzle.
    • A DomainGrid to store sets of possible values for each cell.
    • Track placedCells (pre-filled numbers and their coordinates).
    • A set possibleNumbers representing all numbers from 1 to N*N.

Step 2: Domain Initialization

  1. Domain Reduction:

    • Use updateDomainGrid to calculate valid numbers for empty cells.
    • Place constraints based on Manhattan distances (vertical/horizontal) and diagonal distances.
  2. Identify Missing Numbers:

    • Analyze the placedCells and determine which numbers are missing.
  3. Propagate Constraints:

    • For each missing number:
      • Determine its closest pre-filled number.
      • Update the domains for cells within valid distances.
  4. 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.

Step 3: Path Consistency and Recoverability

  1. Path Consistency:

    • Ensure adjacent numbers (value ±1) can be placed:
      • Check both immediate neighbors and potential future placements.
  2. Recoverability:

    • Verify that each placed number can connect to its successor and predecessor.

Step 4: Evaluation Function

  1. 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).
  2. Purpose:

    • Guide the solver to prioritize the most promising moves.

Step 5: Alpha-Beta Search (Recursive Solver)

  1. 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.
  2. Pruning:

    • Use alpha-beta pruning to cut off unpromising branches:
      • If a candidate's score is worse than alpha, prune.
      • Update alpha when a better score is found.
  3. Backtracking:

    • Recursively attempt to solve the grid.
    • If no solution exists for the current branch, backtrack to explore alternatives.

Step 6: Solution Verification

  1. Check for Completeness:

    • The solver stops when all cells are filled with valid numbers (1 to N*N).
  2. Validate Solution:

    • Compare the generated solution against the expected solution (if available in the input).
  3. Output:

    • Print the grid, execution time, and whether the solution is correct.

Key Techniques Used

  1. Constraint Satisfaction Problem (CSP):

    • The problem is modeled as a grid where constraints enforce number placement.
  2. Alpha-Beta Pruning:

    • Optimizes the search space to avoid redundant computations.
  3. Heuristic Evaluation:

    • A scoring function guides the solver to prioritize promising moves.
  4. Path Consistency:

    • Ensures the solver maintains valid number placements at each step.
  5. Backtracking with Domain Propagation:

    • Incrementally explores possibilities while updating domains to maintain consistency.

Overall Workflow:

  1. Parse input and initialize domains.
  2. Propagate constraints to reduce the search space.
  3. Use recursive alpha-beta search with heuristics to explore potential solutions.
  4. Verify the solution and print the results.

About

A fast Hidoku Solver (500 puzzles in 4 seconds) using CSP with pseudo alpha/beta pruning

Resources

License

Stars

Watchers

Forks

Contributors