OCR Project
Loading...
Searching...
No Matches
solver.c File Reference

8-direction crossword word-search solver. More...

#include "solver.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for solver.c:

Macros

#define N_DIRS   8

Functions

CharGridgrid_create (int rows, int cols)
 Allocate a CharGrid of the given dimensions, filled with spaces.
void grid_fill (CharGrid *grid, const int *labels, size_t count)
 Fill a CharGrid from an array of predicted character indices.
CharGridgrid_load (const char *path)
 Read a CharGrid from a plain-text file.
void grid_print (const CharGrid *g)
 Print a CharGrid to stdout (for debugging / verbose mode).
void grid_free (CharGrid *g)
 Free a CharGrid.
static int try_direction (const CharGrid *grid, const char *word, int r, int c, int dir)
 Check whether word fits in grid starting at (r, c) going in direction dir.
WordResult solver_find (const CharGrid *grid, const char *word)
 Search for word in grid in all 8 directions.
const char * solver_dir_name (int dir)
 Return the human-readable name of direction index dir.

Variables

static const int DIR_DR [N_DIRS] = { 0, 1, 1, 1, 0, -1, -1, -1}
 Row and column deltas for each of the 8 search directions.
static const int DIR_DC [N_DIRS] = { 1, 1, 0, -1, -1, -1, 0, 1}
static const char * DIR_NAMES [N_DIRS]

Detailed Description

8-direction crossword word-search solver.

Macro Definition Documentation

◆ N_DIRS

#define N_DIRS   8

Number of search directions.

Function Documentation

◆ grid_create()

CharGrid * grid_create ( int rows,
int cols )

Allocate a CharGrid of the given dimensions, filled with spaces.

Parameters
rowsNumber of rows.
colsNumber of columns.
Returns
Heap-allocated CharGrid, or NULL on failure. Free with grid_free().
Here is the caller graph for this function:

◆ grid_fill()

void grid_fill ( CharGrid * grid,
const int * labels,
size_t count )

Fill a CharGrid from an array of predicted character indices.

Parameters
gridTarget grid (must already be allocated with the right size).
labelsArray of class indices in [0, 25]; labels[r*cols + c] gives the index for cell (r, c). 0='A', 25='Z'.
countNumber of elements in labels (must equal rows * cols).
Here is the caller graph for this function:

◆ grid_free()

void grid_free ( CharGrid * grid)

Free a CharGrid.

Parameters
gridGrid to free. No-op if NULL.
Here is the caller graph for this function:

◆ grid_load()

CharGrid * grid_load ( const char * path)

Read a CharGrid from a plain-text file.

File format (first line: dimensions, then one row per line):

rows cols
ABCDE
FGHIJ
Parameters
pathPath to the grid file.
Returns
Heap-allocated CharGrid on success, NULL on error. Free with grid_free().
Here is the call graph for this function:

◆ grid_print()

void grid_print ( const CharGrid * grid)

Print a CharGrid to stdout (for debugging / verbose mode).

Parameters
gridGrid to display.
Here is the caller graph for this function:

◆ solver_dir_name()

const char * solver_dir_name ( int dir)

Return the human-readable name of direction index dir.

Directions are numbered 0–7: 0=RIGHT, 1=DOWN_RIGHT, 2=DOWN, 3=DOWN_LEFT, 4=LEFT, 5=UP_LEFT, 6=UP, 7=UP_RIGHT

Parameters
dirDirection index in [0, 7].
Returns
Constant string (e.g. "RIGHT"), or "UNKNOWN" if out of range.
Here is the caller graph for this function:

◆ solver_find()

WordResult solver_find ( const CharGrid * grid,
const char * word )

Search for word in grid in all 8 directions.

The search is case-insensitive; the grid and word are both converted to uppercase before comparison.

Parameters
gridCharacter grid to search in.
wordNull-terminated string to search for (A–Z, case-insensitive).
Returns
WordResult with found=1 and coordinates if found, or found=0 if not present.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_direction()

int try_direction ( const CharGrid * grid,
const char * word,
int r,
int c,
int dir )
static

Check whether word fits in grid starting at (r, c) going in direction dir.

Parameters
gridCharacter grid.
wordUppercase null-terminated search string.
rStarting row.
cStarting column.
dirDirection index (0–7).
Returns
1 if the word matches in that direction, 0 otherwise.
Here is the caller graph for this function:

Variable Documentation

◆ DIR_DC

const int DIR_DC[N_DIRS] = { 1, 1, 0, -1, -1, -1, 0, 1}
static

◆ DIR_DR

const int DIR_DR[N_DIRS] = { 0, 1, 1, 1, 0, -1, -1, -1}
static

Row and column deltas for each of the 8 search directions.

Indexed 0–7: 0 = RIGHT (dr= 0, dc=+1) 1 = DOWN_RIGHT (dr=+1, dc=+1) 2 = DOWN (dr=+1, dc= 0) 3 = DOWN_LEFT (dr=+1, dc=-1) 4 = LEFT (dr= 0, dc=-1) 5 = UP_LEFT (dr=-1, dc=-1) 6 = UP (dr=-1, dc= 0) 7 = UP_RIGHT (dr=-1, dc=+1)

◆ DIR_NAMES

const char* DIR_NAMES[N_DIRS]
static
Initial value:
= {
"RIGHT", "DOWN_RIGHT", "DOWN", "DOWN_LEFT",
"LEFT", "UP_LEFT", "UP", "UP_RIGHT"
}