Cryptarithmetic Puzzle in C++

Cryptarithmetic Puzzles are sometimes also called as verbal arithmetic or alphametics. In these kinds of math-based puzzles, the letters or symbols stand for numbers in an arithmetic equation. The main aim of this puzzle is to determine the right digit that will be used to form a true equation for each letter. With their captivating nature, puzzle lovers and computer scientists cannot forget this class of problems.

Problem Statement:

Consider the example: "SEND + MORE = MONEY". Each letter represents a unique digit from 0 to 9, and the task is to find the correct assignment of digits to letters to make the equation valid. This problem falls under the category of constraint satisfaction problems, where we must satisfy a set of constraints (the arithmetic equation) subject to certain conditions (each letter representing a different digit).

Approach:

One common approach to solving cryptarithmetic puzzles is brute force enumeration combined with backtracking. We systematically try different combinations of digit assignments until a valid solution is found or all possibilities are exhausted.

Algorithm Overview:

  1. Generate Permutations: Enumerate all possible permutations of digits for the given letters.
  2. Check Validity: For each permutation, substitute the digits into the equation and check if it holds true.
  3. Backtracking: If the equation is not satisfied, backtrack and try a different permutation until a solution is found or all permutations are exhausted.

Program 1: Brute Force Backtracking Implementation

Let us take an example to illustrate the cryptarithmetic puzzle using the Brute Force Backtracking in C++.

Output:

Solution found:  D = 1 E = 5 M = 0 N = 3 O = 8 R = 2 S = 7 Y = 6

Explanation:

  • In this implementation, the problem is solved using the technique of backtracking.
  • There is a LetterMapping structure to map every letter in a digit.
  • There is an evaluation function to determine if the underlying equation satisfies the current mapping.
  • First, solveCryptographic examines whether two strings have at most ten unique characters that would make it possible to solve it. After that, it calls permutation which initializes mappings and tries all permutations.
  • A permutation function recursively explores all feasible 'digit-letter' permutations until it finds one that works.
  • If the solution exists, print out the answer.

Program 2: Constraint Satisfaction Problem (CSP) Implementation using OR-Tools

Let us take another example to illustrate the cryptarithmetic puzzle using the Constraint Satisfaction Problem in C++.

Output:

S = 9
E = 5
N = 6
D = 7
M = 1
O = 0
R = 8
Y = 2
Carries: 0 1 0 1

Explanation:

  • This implementation uses the OR-Tools library as a CSP to solve the puzzle.
  • After that, it defines variables for each letter and carries.
  • Constraints are set up to ensure that the equation is satisfied.
  • Using a constraint solver, it looks for a solution and prints it if found.

Program 3: Constraint Propagation Implementation

Let us take another example to illustrate the cryptarithmetic puzzle using the Constraint Propagation in C++.

Output:

No solution found

Explanation:

  • This implementation solves puzzles through constraint propagation, thereby avoiding guesswork by other algorithms.
  • A solveCryptarithmeticWithPropagation recursive function exists to assign values to characters while respecting constraints.
  • While keeping track of what digits are already used and if the present assignment is still valid.
  • Digits should be assigned to letters with many constraints on them first for efficiency purposes.
  • The main method performs puzzle initialization and solving using solveCryptarithmeticPuzzleWithPropagation function call.
  • If found as a solution, display it.

Conclusion:

Cryptarithmetic puzzles not only entertain challenges but also serve as excellent exercises in problem-solving and algorithm design. Implementing a solver for these puzzles in C++ provides a hands-on opportunity to explore concepts like backtracking, permutation generation, and constraint satisfaction.






Latest Courses