Variable without initialization returns segmentation fault

79 views Asked by At

Description: A friend of you is doing research on the Traveling Knight Problem (TKP)where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

There are multiple test cases. The first line contains an integer T, indicating the number of test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

For each test case, print one line saying "To get from xx to yy takes n knight moves.". */

class Graph{
    int V;
    list<int> *adj;
public:
    //constructor, use adjacent list to represent the graph        
Graph(int V){
    V = V;
    adjacent = new list<int>[V];
}
void add(int i,int j){
    adjacent[i].push_back(j);
    adjacent[j].push_back(i);
}
// build the chess board
void add(int i){
    if(i + 1 >= 0 && i + 1 <= 63 && ((i + 1) / 8 == i / 8))                              
        add(i,i + 1);
    if(i + 7 >= 0 && i + 7 <= 63 && ((i + 7) / 8 == i / 8 + 1))
        add(i,i + 7);
    if(i + 8 >= 0 && i + 8 <= 63 && ((i + 8) / 8 == i / 8 + 1))
        add(i,i + 8);
    if(i + 9 >= 0 && i + 9 <= 63 && ((i + 9) / 8 == i / 8 + 1))
        add(i,i + 9);
}
//use BFS to find the shortest path from s to e
int BFS(int s ,int e){
    vector<bool> visited ;
    for(int i = 0; i < V; i++)
        visited.push_back(false);
    vector<int> distances;
    for(int i = 0; i < V; i++)
        distances.push_back(-1);
    distances[s] = 0;
    for(int current = 0; current < V; current++){
        for(int i = 0; i < V; i++){
            if( !visited[i] && distances[i] == current){
                visited[i] = true;
                if(i == e){
                    return distances[i];
                }
                for(list<int>::const_iterator k = adj[i].begin(); 
                    k != adj[i].end(); k++){
                    if(distances[*k] == -1){
                        distances[*k] = current + 1;
                    }
                }
            }
        }
   }
   return 0;
}
};
//test the BFS
int main(){
    Graph g(64);
    for(int i = 0; i < 64; i++){
        g.add(i);
    }
    cout << g.BFS(1,2);
    return 0;     

}

1

There are 1 answers

0
R Sahu On BEST ANSWER

The following code leaves the member variable V in an uninitialized state, which is probably the cause of your problems.

Graph(int V){
    V = V; // The LHS is the same as the RHS, the argument to the function
           // not the class member variable.
    adjacent = new list<int>[V];
}

Use:

Graph(int V) : V(V) {
    adjacent = new list<int>[V];
}

To make the code easier to read, use a different name for the argument.

Graph(int inV) : V(inV) {
    adjacent = new list<int>[V];
}