/*------------------------------------------------------------------------------ * problem002.c: Help Jenny Cub find the honey in Roseland Park. **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #include #include #include /*------------------------------------------------------------------------------ * Data Structures **----------------------------------------------------------------------------*/ struct map { int rows; int cols; char **data; }; typedef struct map Map; /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define MAX_STRLEN 128 #define TRUE 1 #define FALSE 0 enum { N = 0, NE, E, SE, S, SW, W, NW, NDIRECTIONS }; const int DIR_STEPS[NDIRECTIONS][2] = { { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 } }; /*------------------------------------------------------------------------------ * Macros **----------------------------------------------------------------------------*/ #define chomp(s) (s)[strlen(s) - 1] = 0 /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ Map* read_map() { Map *map; int r, c; map = malloc(sizeof(Map)); scanf("%d %d\n", &(map->rows), &(map->cols)); map->data = malloc(sizeof(char*) * map->rows); for (r = 0; r < map->rows; r++) { map->data[r] = malloc(sizeof(char) * map->cols); for (c = 0; c < map->cols; c++) map->data[r][c] = getchar(); getchar(); } return map; } void free_map(Map *map) { int r; if (map) { if (map->data) { for (r = 0; r < map->rows; r++) free(map->data[r]); free(map->data); } free(map); } } void search_map(Map *map, char *flower) { int r, c, d; for (r = 0; r < map->rows; r++) for (c = 0; c < map->cols; c++) if (tolower(map->data[r][c]) == tolower(*flower)) { for (d = 0; d < NDIRECTIONS; d++) if (search_direction(map, flower, r, c, d)) return; } printf("%s Not Found\n", flower); } int search_direction(Map *map, char *flower, int srow, int scol, int dir) { int r, c, rstep, cstep, i; char *sptr; rstep = DIR_STEPS[dir][0]; cstep = DIR_STEPS[dir][1]; sptr = flower; i = 0; r = srow; c = scol; while (0 <= r && r < map->rows && 0 <= c && c < map->cols) { if (tolower(map->data[r][c]) != tolower(*sptr++)) return FALSE; if (! *sptr) break; r += rstep; c += cstep; i++; } if (i < strlen(sptr)) return FALSE; printf("%s (%d, %d) (%d, %d)\n", flower, srow + 1, scol + 1, r + 1, c + 1); return TRUE; } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { Map *map; int m, f, nmaps, nflowers; char flower[MAX_STRLEN]; scanf("%d", &nmaps); for (m = 1; m <= nmaps; m++) { printf("Map %d\n", m); map = read_map(); scanf("%d\n", &nflowers); for (f = 0; f < nflowers; f++) { fgets(flower, MAX_STRLEN, stdin); chomp(flower); search_map(map, flower); } free_map(map); if (m < nmaps) putchar('\n'); } return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=c **----------------------------------------------------------------------------*/