-1

Currently I am trying to create a little text based dungeon crawler in C where the map should be randomly generated. I try to accomplish this by using a quad-linked list, where every node(room) can have up to four connections to next rooms.

typedef struct Room {
    int x; //each room got its own number to be identified.
    struct Room *north;
    struct Room *east;
    struct Room *south;
    struct Room *west; } room;

It should also be possible for some rooms to have only one or two or three connections while the unused pointers to next nodes remains NULL. For varius reasons I need a search algorithm which iterates through the rooms to find a specific one. I have no idea how to implement something like this. Any ideas?

Markus
  • 93
  • 6
  • Are you searching the rooms based on the `x` field ? Then you can just initialize the values of each room serially (row wise) and the search becomes trivial. – a_pradhan Feb 25 '15 at 20:07
  • Add an 'already searched' bool and recursively hunt around? You may be better off with two bools, and alternate between them on each search so that you can reset one set while searching the other. – Martin James Feb 25 '15 at 20:09
  • @Akash Pradhan: I don't think this will work. If I want the program to "find room Nr. 47" it should not only find the index, it should also find the location. To do this, it has to iterate quad-directional through each room one by one. – Markus Feb 25 '15 at 20:18
  • 1
    i think you are trying to solve the standard "Escape the maze" problem – arunmoezhi Feb 25 '15 at 20:18
  • @Martin James yeah that could work. Thanks. – Markus Feb 25 '15 at 20:18
  • 1
    This "question", which in fact isn't one, doesn't seem to be appropriate for SO. What have you tried? Why don't any of the standard graph search techniques BFS, DFS ... suffice? – Jens Gustedt Feb 25 '15 at 20:19

3 Answers3

2

I think You can use a depth first search technique for finding the desired value.As there may be possible four side to found so this algorithm will help You to find.

For learning DFS (depth first search) You can read these :)

http://en.wikipedia.org/wiki/Depth-first_search

http://www.ics.uci.edu/~eppstein/161/960215.html

http://www.geeksforgeeks.org/applications-of-depth-first-search/

Robin Halder
  • 253
  • 2
  • 14
1

A simple solution would be to create an array of Room, or an array of Room *

Then, you can search in the array with a loop, until you find the room with the searched value of x, or reach the end of the array.


If you are starting from an initial room, you can try the recursive search of a chained list.

However, we must add a bool in the struct Room to avoid infinite search, because your rooms might loop on each other like this :

 O--O
 |  |
 O--O
  O : room, - and | : link between rooms

The checked value should be initialised to false before starting to search, and that parameter gets to true when the room is checked once.

principle :

Room* search_room (int x_search, Room* current_room)
{
//if current_room is NULL, return NULL
//if checked is true, return NULL
//checked becomes true
//if current_room->x equals x_search, return current_room

//store the result of search_room(x_search, current_room->north) somewhere
//if it's not null, return what it has returned
//if not, then proceed to the store and check of the 3 others calls :
//search_room(x_search, current_room->east)
//search_room(x_search, current_room->south)
//search_room(x_search, current_room->west)

//if all the 4 calls returned NULL, return NULL
}
Red-Jeweled
  • 251
  • 4
  • 10
1

It will be easy with a recursive algorithm, treat your quad list as of graph which have left, right , top and bottom connection.Following is the pseudo code for your searching algorithm:-

typedef enum{
    VISITED,
    NOT_VISITED
}status;
typedef struct vertex{
    int x;
    struct vertex* up;
    struct vertex* down;
    struct vertex* left;
    struct vertex* right;
    status node_status;
}node;
typedef struct{
    node* g_nodes;
    int capacity, length, width,curr_filled;
    int row_index, col_index;
}graph;
g_node* search_graph (graph* gph, int key)
{
    static g_node = get_first_node ( gph ); //you can get any node.
    if ( g_node->x == key){
        return g_node;
    }
    if ( g_node->node_status == VISITED){
        return NULL;
    } 
    g_node->node_status = VISITED;
    if (g_node->left != NULL){
        return search_graph (gph, g_node->left);
    }
    if (g_node->right != NULL){
        return print_graph (gph, g_node->right);
    }
    if (g_node->up != NULL){
        return print_graph (gph, g_node->up);
    }
    if (g_node->down != NULL){
        return print_graph (gph, g_node->down);
    }

}  
OldSchool
  • 2,123
  • 4
  • 23
  • 45