Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created April 8, 2012 13:50
Show Gist options
  • Save HappyCerberus/2337419 to your computer and use it in GitHub Desktop.
Save HappyCerberus/2337419 to your computer and use it in GitHub Desktop.
Vzorove reseni bludiste (jendotlive faze = jednotlive verze souboru)
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int search(char **bludiste, int y, int x)
{
/* cil */
if (bludiste[y][x] == 'C') return 1;
/* policko neni volne, ani start */
if (bludiste[y][x] != ' ' && bludiste[y][x] != 'S') return 0;
if (bludiste[y][x] == ' ') /* aby jsme si neprepsali start */
bludiste[y][x] = '*';
/* prohledani 4 smeru + propagace uspechu */
if (search(bludiste,y-1,x) == 1) return 1;
if (search(bludiste,y+1,x) == 1) return 1;
if (search(bludiste,y,x-1) == 1) return 1;
if (search(bludiste,y,x+1) == 1) return 1;
if (bludiste[y][x] == '*')
bludiste[y][x] = ' ';
/* vsechny cesty z tohoto policka jsou slepe */
return 0;
}
/* pro jednoduchost pouzivame
* pro pozici znaku 'S' globalni promenne */
int s_col = 0;
int s_row = 0;
/** \brief Funkce pro nacteni jednoho radku bludiste */
char *getline(int *cols, int row)
{
char *buf = NULL;
int index = 0;
int kapacita = 0;
int c; /* pozor, protoze pouzivame getchar, musime tady mit int */
while ((c = getchar()) != '\n' && c != EOF)
{
if (index == kapacita) /* pokud uz nemame misto, musime provest realokaci */
{
if (kapacita == 0)
{
/* optimalizace - pokud mame delku radku, tak delame jenom jednu alokaci */
if (*cols != 0) kapacita = *cols;
else kapacita = 10;
}
else kapacita *= 1.5;
buf = realloc(buf,kapacita);
assert(buf != NULL); /* nedostatek pameti je fatal error */
}
if (c == 'S') /* detekce pocatku */
{
s_col = index;
s_row = row;
}
buf[index] = c;
index++;
}
if (*cols == 0) /* menit muzeme pouze pokud v cols neni hodnota */
/* jinak by vzdy posledni radek (s EOF) prepsal cols na 0 */
*cols = index; /* propagace poctu nactenych znaku */
return buf;
}
/** \brief Funkce pro nacteni bludiste
*
* Logika funkce kopiruje getline()
* - misto znaku nacitame radky
* - propagujeme ven pocet radku
*/
char **getbludiste(int *cols, int *rows)
{
char **buf = NULL;
int index = 0;
int kapacita = 0;
char *c; /* nacitame radky */
while ((c = getline(cols,index)) != NULL)
{
if (index == kapacita) /* pokud uz nemame misto, musime provest realokaci */
{
if (kapacita == 0) kapacita = 10;
else kapacita *= 1.5;
buf = realloc(buf,kapacita*sizeof(char*));
assert(buf != NULL); /* nedostatek pameti je fatal error */
}
buf[index] = c;
index++;
}
*rows = index; /* propagace poctu nactenych radku */
return buf;
}
int main()
{
int cols = 0;
int rows = 0;
char **bludiste = getbludiste(&cols, &rows);
if (bludiste == NULL) return EXIT_FAILURE;
search(bludiste,s_row,s_col);
for (int j = 0; j < rows; j++)
{
for (int i = 0; i < cols; i++)
putchar(bludiste[j][i]);
putchar('\n');
free(bludiste[j]);
}
free(bludiste);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment