Figure 9: Partial implementation of breadth-first algorithm for robot navigation problem

#define BLOCKED 1
#define CLEAR   0
#define H       4
#define V       4


int matrix[H][V] =
   { {1, 0, 0, 0},
     {0, 0, 1, 0},
     {0, 1, 0, 0},
     {1, 0, 0, 0} };

struct node {
   int    number;
   int    x;
   int    y;
   struct node *parent;
   struct node *next;
};
   
struct node *CLOSED, *down, *left, 
            *n, *OPEN, *right;

// Initialize this problem.

OPEN   = NULL;
CLOSED = NULL;

n = create_new_node(node_number, 1, 0);
OPEN = n;
node_number++;

n = create_new_node(node_number, 2, 0);
add_to_list(&OPEN, &n);
node_number++;

n = create_new_node(node_number, 3, 0);
add_to_list(&OPEN, &n);
node_number++;


while(searching){

   if(is_empty(OPEN))
      no_solution_exists(&searching);

   else{  /* OPEN list is not empty */

      n = remove_from_list(&OPEN);
      add_to_list(&CLOSED, &n);

// EXPAND NODE N
// First try to go downwards. If that is possible,
// don't expand anymore.

      if(can_go_downwards(n)){
         down = new_down_node(&n, node_number);
         node_number++;
         if(is_a_goal_node(down)) 
            solution_found(down, &searching);
         else
            add_to_list(&OPEN, &down);
      }  /* ends if can_go_downwards */

// If cannot go downwards, expand left and right.

      else{  /* cannot go downwards */
         if(can_go_left(n)){
            left = new_left_node(&n, node_number);
            node_number++;
            if(is_clear(left))
               add_to_list(&OPEN, &left);
         }  /* ends if can_go_left */

         if(can_go_right(n)){
            right = new_right_node(&n, node_number);
            node_number++;
            if(is_clear(right))
               add_to_list(&OPEN, &right);
         }  /* ends if can_go_right */

      }  /* ends else cannot go downwards */

   }  /* ends else OPEN is not empty */

}  /* ends while searching */