Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2013 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4670191 to your computer and use it in GitHub Desktop.
Save anonymous/4670191 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define NROWS 5
#define NCOLS 5
/* The array I'll be walking through */
int array2d[NROWS][NCOLS] = {{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
struct Node
{
int row;
int col;
struct Node* direction[4];
};
int nodetotal = 0;
struct Node* initNode()
{
printf("Making node %i\n", nodetotal++);
struct Node *tmp = (struct Node *)malloc(sizeof(struct Node));
tmp->direction[0] = NULL;
tmp->direction[1] = NULL;
tmp->direction[2] = NULL;
tmp->direction[3] = NULL;
return tmp;
}
int oppDirection(int direction)
{
/* Returns the opposite direction
eg. North returns South, West
returns East, etc. */
if (direction == 0) {return 2;}
else if (direction == 1) {return 4;}
else if (direction == 2) {return 0;}
else if (direction == 3) {return 1;}
}
int limiter = 0;
void walk(struct Node *node)
{
//if (++limiter > 5) {return;}
struct Node *nextnode;
printf("Starting walk %i\n", limiter++);
printf("Row: %i Col: %i, Memory: %p\n", node->row, node->col, node);
/* Look to see if North is a legit direction and unvisited */
if ((node->row != 0) && (node->direction[0] == 0))
{
printf("North is clear\n");
nextnode = initNode();
nextnode->row = node->row - 1;
nextnode->col = node->col;
/* The next node's South is the current node */
nextnode->direction[2] = node;
/* The current node's North is the newly created node */
node->direction[0] = nextnode;
printf("Taking a walk North\n");
walk(nextnode);
}
/* Look to see if East is a legit direction and unvisited */
if ((node->col < NCOLS - 1) && (node->direction[1] == 0))
{
printf("East is clear\n");
nextnode = initNode();
nextnode->col = node->col + 1;
nextnode->row = node->row;
/* The next node's West is the current node */
nextnode->direction[3] = node;
/* The current node's East is the newly created node */
node->direction[1] = nextnode;
walk(nextnode);
}
/* Look to see if South is a legit direction and unvisited */
if ((node->row < NROWS - 1) && (node->direction[2] == 0))
{
printf("South is clear\n");
nextnode = initNode();
nextnode->row = node->row + 1;
nextnode->col = node->col;
/* The next node's North is the current node */
nextnode->direction[0] = node;
/* The current node's South is the newly created node */
node->direction[2] = nextnode;
walk(nextnode);
}
/* Look to see if West is a legit direction and unvisited */
if ((node->col != 0) && (node->direction[3] == 0))
{
printf("West is clear\n");
nextnode = initNode();
nextnode->col = node->col - 1;
nextnode->row = node->row;
/* The next node's East is the current node */
nextnode->direction[1] = node;
/* The current node's West is the newly created node */
node->direction[3] = nextnode;
walk(nextnode);
}
return;
}
int main(int argc, char *argv)
{
/* Here's our starting point at 0,0 */
struct Node *head = initNode();
head->row = 0;
head->col = 0;
/* No freeing memory yet... I'll figure that out... */
walk(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment