-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatinSolver.c
88 lines (80 loc) · 2.64 KB
/
latinSolver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* @file latinSolver.c
*
* @brief Main program for solving a Latin square puzzle using backtracking.
*
* This program initializes a Latin square puzzle from an input file, then attempts
* to solve it using backtracking with the help of a stack. It displays the outcome
* of the puzzle solution, including the number of push and pop operations performed.
*
* Usage:
* ```
* ./latinSolver <inputFile.txt>
* ```
*
* @authors
* - Panagiotis Tsembekis
* - Rafael Tsekouronas
*
* @bug No known bugs.
*/
#include <stdlib.h>
#include <stdio.h>
#include "file.h"
#include "stack.h"
#include "solver.h"
/**
* @brief Entry point of the Latin square puzzle solver program.
*
* This function initializes the puzzle from an input file, sets up a stack
* for backtracking, and calls the `puzzleSolver` function to solve the puzzle.
* It then outputs the result of the solution attempt, including the number of
* pushes and pops performed. If any errors occur (e.g., memory allocation failure
* or an unsolvable puzzle), an appropriate message is displayed.
*
* @param argc The argument count. Should be 2 (program name and input file name).
* @param argv The argument vector, where `argv[1]` is expected to be the input file name.
*
* @return 0 if the puzzle was solved successfully, 1 if the puzzle was unsolvable or an error occurred.
*/
int main(int argc, char *argv[])
{
if (argc != 2) // check for improper arguments passed
{
printf("Usage:\n");
printf("./latinSolver <inputFile.txt>\n");
return 1;
}
const char *fileName = argv[1];
int size;
int **tableau = readLatinSquare(fileName, &size); // read initial latinsquare and save to tableau
if (tableau == NULL) // handle improper initialization
{
perror("Failed to initialize puzzle from input file.\n");
return 1;
}
STACK *stack;
if (initStack(&stack) == EXIT_FAILURE) // handle improper alloaction for stack
{
free2DArray(tableau, size); // free previously allocated 2D array
perror("Error initializing stack.\n");
return 1;
}
int numPush = 0, numPop = 0;
bool gameResult = puzzleSolver(stack, tableau, size, &numPush, &numPop); // call backtracking funtion to solve
// Check return value of the solver
if (gameResult)
{
printf("Congrats! Puzzle solved!\n");
printf("PUSH NUM: %d\n", numPush);
printf("POP NUM: %d\n", numPop);
}
else
{
printf("Puzzle not solved. (unsolvable or error)\n");
}
// Free previously allocated memory blocks
free2DArray(tableau, size);
freeStack(stack);
return 0;
}