@mainpage
A collaborative project by Panagiotis Tsembekis (UC1070326) and Rafael Tsekouronas (UC1070153).
This project implements a backtracking algorithm using a stack to solve the Latin Square puzzle. A Latin Square is an ( n \times n ) grid where each row and column contains the numbers from 1 to ( n ) exactly once. The program reads an initial puzzle configuration from a file, solves it, and outputs the solution step-by-step.
- Backtracking algorithm for puzzle solving.
- Dynamically allocated 2D arrays for grid representation.
- Stack-based backtracking to explore possible solutions.
- Handles puzzles of various sizes as specified in the input file.
- Reports all memory usage and leaks using
valgrind
. - Modular structure with separate
.c
and.h
files for:- Stack management.
- File handling.
- Solver logic.
- Clear and informative error messages and outputs.
- Handling of invalid .txt files passed in arguments for initial tableau.
file.c
/file.h
: Handles reading the puzzle from a file and memory management of dynamic arrays.stack.c
/stack.h
: Manages the stack operations (push, pop, and memory deallocation).solver.c
/solver.h
: Implements the backtracking algorithm to solve the Latin Square.latinSolver.c
: Contains the main function and integrates the modules.
lsq2.txt
: Another example input file for a 4x4 Latin Square.4 0 0 0 -4 0 -2 0 0 0 0 0 0 0 0 0 -2
lsq3.txt
: Example input file for a 4x4 Latin Square with some cells pre-filled.7 0 0 0 -4 0 -6 0 0 -2 0 0 -1 -7 0 0 -1 0 0 0 -3 0 0 0 0 -2 0 0 0 -6 -4 0 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 0 -5 0 -7
- The program prints the step-by-step solution to the console, including:
- Current grid state for each step.
- Push and pop operations on the stack.
- Number of total
push
andpop
operations performed when the puzzle is solved.
To compile the program, use the provided Makefile
:
make
Run the solver with an input file:
./latinSolver <input_file.txt>
Example:
./latinSolver lsq1.txt
All dynamically allocated memory is freed before program termination. Use valgrind
to verify memory safety:
valgrind --leak-check=full ./latinSolver <input_file>
Doxygen comments are included throughout the source files. To generate documentation:
- Run the following command:
doxygen Doxyfile
- Open the
html/index.html
file in your browser for detailed documentation.
4
0 -2 0 -4
-2 -3 0 0
0 0 -1 -2
0 0 0 -3
PUSH: STEP 1
+-----+-----+-----+-----+
| 1 | (2) | 0 | (4) |
+-----+-----+-----+-----+
...
Congrats! Puzzle solved!
PUSH NUM: 9
POP NUM: 0
Test the solver with provided input files (lsq2.txt
and lsq3.txt
) or create your own. Ensure the input format matches:
- The first line specifies the size ( n ) of the grid.
- Subsequent lines contain ( n ) integers per line, separated by spaces.