Skip to content

Instantly share code, notes, and snippets.

@shelling
Created December 21, 2009 16:39
Show Gist options
  • Select an option

  • Save shelling/261050 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/261050 to your computer and use it in GitHub Desktop.
famous maze problem
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#include <stdbool.h>
void printfile( FILE *file ) {
if ( file != NULL ) {
rewind( file );
int c = fgetc( file );
while ( c != EOF ) {
printf( "%c", c );
c = fgetc( file );
}
}
printf("\n");
rewind( file );
}
void inspect_maze_matrix( char array[][9] ) {
printf("[\n");
for( int i = 0; i < 9; i++ ) {
printf(" [ ");
for( int j = 0; j < 9; j++ ) {
printf( "%c ", array[i][j] );
}
printf("]\n");
}
printf("]\n");
}
void fill_maze_matrix( char array[][9] ) {
FILE *maze_file = fopen( "data.txt", "r" );
// printfile( maze_file );
int x = 0;
int y = 0;
char c = fgetc( maze_file );
while ( c != EOF ) {
if ( c == *"X" ) {
array[x][y] = *"X";
y++;
} else if ( c == *"Y" ) {
array[x][y] = *"Y";
y++;
} else if ( c == *"\n" ) {
x++;
y = 0;
}
c = fgetc( maze_file );
}
fclose( maze_file );
}
typedef struct {
int x;
int y;
} Position;
typedef struct {
} PositionStack;
void print_position( Position *pos ) {
printf( "current position: x:%d y:%d\n", pos->x, pos->y );
}
bool valid_position( Position *pos ) {
if (
pos->x < 9 &&
pos->x >= 0 &&
pos->y < 9 &&
pos->y >= 0
) {
return true;
} else {
return false;
}
}
bool search( char array[][9], Position *current_pos ) {
if ( current_pos->x == 8 && current_pos->y == 0 ) {
print_position( current_pos );
return true;
} else {
if ( array[ current_pos->x ][ current_pos->y ] != *"Y" ) {
return false;
} else {
print_position( current_pos );
Position below = { current_pos->x + 1, current_pos->y };
Position above = { current_pos->x - 1, current_pos->y };
Position left = { current_pos->x, current_pos->y - 1 };
Position right = { current_pos->x, current_pos->y + 1 };
if ( valid_position( &below ) && search( array, &below ) ) {
return true;
}
else if ( valid_position( &left ) && search( array, &left ) ) {
return true;
}
else {
return false;
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include "as10.h"
char maze[9][9];
int main(void) {
fill_maze_matrix( maze );
inspect_maze_matrix( maze );
Position init_pos = { 0, 8 };
bool can_find_path = search( maze, &init_pos );
if ( !can_find_path ) {
printf( "INFO - We could not find out a possible path to go through the maze." );
}
return 0;
}
#!/usr/bin/env perl
#
use Term::ANSIColor qw(:constants);
open $data, " < data.txt" or die $!;
while (<$data>) {
$y = RED("Y"). RESET;
s/Y/$y/g;
print $_;
}
print "\n";
X X Y Y Y Y X Y Y
Y Y Y Y X Y Y X Y
X X Y X Y Y Y X Y
Y Y Y Y X X Y X Y
X Y X Y Y X Y Y Y
X Y X X Y X X X X
Y Y X Y X Y Y Y Y
X Y Y Y X Y X X Y
Y Y X Y Y Y X Y Y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment