closed account (Eh5iz8AR)
Hey guys, first time posting to this site. Just to forewarn you, this is a homework question but i'm not looking for a solution, simply a push in the right direction.

The problem deals with a Breadth First Search on a hexagonal grid, where I have a 22x22 2D array that will determine the shortest path between the two points of Duck Dodgers and Marvin Martian. The grid consists of -1's -2's and the goal which is a 0.

The -1's are empty spaces you can move to, -2's are lava which you can't move to, and the 0 is the goal called Illudium Phosdex.

The part where I am having trouble is when I need to start at the goal (0) and cascade numbers around its 6 neighbors by +1 each time. This picture is the 20x20 grid and an example on what I need to do with it.

http://i.imgur.com/chYI4.png

To get an idea why I need to do this, while using a breadth first search function called findPath I am starting at either Duck Dodgers or Marvin Martian, processNeighbors, and taking their (current path - 1) and enqueueing it to my Queue q. It will keep doing processNeighbor and enqueueing a closer number until the path is reached. I don't know how to do this part and would really appreciate some help.

Heres my queue.cc:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138`` ``````#include "queue.h" #include using namespace std; const int ROW = 22; const int COL = 22; int numberCells(int r, int c); int map[ROW][COL]; int main(void) { int duckX, duckY, marvinX, marvinY, illudiumX, illudiumY, lavaX, lavaY; int r,c,nLava; for (r=0; r> duckY; cout << "Please enter Duck Dodger's X coordinate: "; cin >> duckX; cout << "\nPlease enter Marvin the Martian's Y coordinate: "; cin >> marvinY; cout << "Please enter Marvin the Martian's X coordinate: "; cin >> marvinX; cout << "\nPlease enter the Illudium Phosdex's Y coordinate: "; cin >> illudiumY; cout << "Please enter the Illudium Phosdex's X coordinate: "; cin >> illudiumX; illudiumX++; illudiumY++; duckX++; duckY++; marvinX++; marvinY++; // check for validity here if you want cout << "Enter number of lava locations: "; cin >> nLava; for (int i=0;i> lavaY; cout << "Enter lava X location: "; cin >> lavaX; lavaX++; lavaY++; // mark lava location map[lavaY][lavaX] = -2; } map[illudiumY][illudiumX] = 0; for (r=0; r

And my queue.h

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#ifndef _QUEUE_H #define _QUEUE_H const int MAX_QUEUE_SIZE = 20; enum QueueExceptions { E_QUEUE_EMPTY, E_QUEUE_FULL, E_ARRAY_OUT_OF_BOUNDS }; class Queue { public: Queue(void) { head = 0; tail = 0; count = 0; } ~Queue(void) { } void clear(void) { head = 0; tail = 0; count = 0; } bool isEmpty(void) { return !count; } int size(void) { return count; } void Queue::enqueue(int d) throw (int) { if (count == MAX_QUEUE_SIZE) throw E_QUEUE_FULL; data[tail] = d; tail = (tail + 1) % MAX_QUEUE_SIZE; count++; } int Queue::dequeue(void) throw(int) { int d; if (count == 0) throw E_QUEUE_EMPTY; d = data[head]; head = (head + 1) % MAX_QUEUE_SIZE; count--; return d; } // void enqueue(int) throw (int); // int dequeue(void) throw (int); int numberCells(int r, int c); private: int data[MAX_QUEUE_SIZE]; int head,tail,count; }; #endif ``````
Last edited on
Topic archived. No new replies allowed.