42 Piscine Rush01 project.
This repository contains my solution for the Rush01 project from the 42 Piscine.
The goal of the project is to solve a 4x4 Skyscraper puzzle using C.
Rush01 is a puzzle where a 4x4 grid must be filled with numbers from 1 to 4.
Each row and each column must contain every number only once:
1 2 3 4The program receives 16 outside clues as a single argument.
These clues describe how many boxes are visible from each side of the grid.
Example:
./rush01 "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2"Expected output example:
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3If the input is invalid or no solution exists, the program prints:
ErrorThe input must contain exactly 16 numbers from 1 to 4, separated by single spaces.
The clues are ordered like this:
col1top col2top col3top col4top
col1bottom col2bottom col3bottom col4bottom
row1left row2left row3left row4left
row1right row2right row3right row4rightIn the program, they are stored in views[16]:
views[0..3] = top views
views[4..7] = bottom views
views[8..11] = left views
views[12..15] = right viewsThe project uses a backtracking algorithm.
Backtracking means:
try a number
continue solving
if it fails later, undo the choice
try another numberThe solver fills the grid one position at a time.
pos 0 -> grid[0][0]
pos 1 -> grid[0][1]
pos 2 -> grid[0][2]
pos 3 -> grid[0][3]
...
pos 15 -> grid[3][3]
pos 16 -> grid is full, check all viewsFor each position, the solver tries numbers from 1 to 4.
A number is valid if it does not already exist in the same row or column.
When the grid is full, the program checks whether all outside views match.
If they do, the grid is printed.
If they do not, the solver backtracks and tries another possibility.
ex00/
├── check.c
├── init_grid.c
├── main.c
├── parse_input.c
├── print_error.c
├── print_grid.c
└── solve.cReads the argument string and stores the 16 clues inside views[16].
It checks that:
there are exactly 16 numbers
numbers are between 1 and 4
numbers are separated by spacesInitializes the 4x4 grid with zeroes.
A zero means the cell is empty.
Fills the grid using backtracking.
It tries numbers from 1 to 4, checks if they are valid, and recursively moves to the next position.
Checks if a number can be placed in a cell without repeating in the same row or column.
Checks if the completed grid matches all outside clues.
Counts how many boxes are visible from one direction.
Prints the solved grid.
Prints:
ErrorCompile from inside the ex00 folder:
cc -Wall -Wextra -Werror -o rush01 *.cRun:
./rush01 "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2"Only allowed external functions are used.
The program does not use printf, atoi, malloc, or free.
The grid size is fixed to 4x4 for the mandatory part of the project.
A future version of this project will generalize the solver from a fixed 4x4 grid to variable grid sizes up to 9x9.
KruKuma