Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

algorithm - Display coordinates (show only path coordinates) in Maze problem using stack in c++

I am implementing rat in maze problem using a stack (assignment requirement) in C++. When I display coordinates it will display all visited/pop up/ moving path coordinates; however, I just want to display those coordinates through which the rat moves from source to destination.

Can anybody tell me what I am doing wrong?

//function to print cordinates
int printPath(pair<int, int>  &path)
{
    cout << "(" << path.first << "," << path.second << ")";
    return path.first && path.second;   
}


//function that will count moves/ find directions and move next etc
bool isReachable(int maze[SIZE][SIZE] , int& count)
{
    // Initially starting at (0, 0).
    
    pair<int, int> foo;

    bool visited[SIZE][SIZE];

    int i = 0, j = 0;

    cout << endl;
    cout <<"
" <<"Start at cordinate" << "("<<i<< "," << j << ")"<<endl;
    cout << endl;
    //int count = 0;
    stack<node> s;
    
    

    node temp(i, j);
    s.push(temp);
    //cout << i << j;

    while (!s.empty()) {

        // Pop the top node and move to  
        // left, right, top, down or  
        // backtrack according the value of node's 
        // pos variable. 
    
        
        temp = s.top();

        
        int d = temp.pos;
        i = temp.x, j = temp.y;
        

        // Increment the direction and 
        // push the node in the stack again. 
        temp.pos++;
        s.pop();
        s.push(temp);

        // If we reach the coordinates 
        // return true 
        if (i == SIZE-1 and j == SIZE-1) {
            count++;
            cout << endl;   
            
            cout << "
"<<"Exit at coordinate" <<"(" <<i <<","<<j <<")"<< endl;
            maze[i][j]=3;
            return true;
        }

        // Checking the Up direction. 
        if (d == 0)
        {
            if (i - 1 >= 0 && maze[i - 1][j] == 0 && visited[i - 1][j])
            {
                

                node temp1(i - 1, j);
                

                visited[i - 1][j] = false;


                foo = make_pair(i, j);
                        
               s.push(temp1);
             
               count++;
               maze[i][j] = 3;
               
    
            }
        }

        // Checking the left direction 
        else if (d == 1)
        {
            if (j - 1 >= 0 && maze[i][j - 1] == 0 && visited[i][j - 1])
            {

                node temp1(i, j - 1);
                
                visited[i][j - 1] = false;


               foo = make_pair(i, j);

    
                s.push(temp1);
                 count++; 
                maze[i][j] = 3;
        

            }
        }

        // Checking the down direction 
        else if (d == 2)
        {
            if (i + 1 < SIZE && maze[i + 1][j] == 0 && visited[i + 1][j])
            {

                node temp1(i + 1, j);
                

                visited[i + 1][j] = false;


                    foo = make_pair(i, j);



                
                
                                
                s.push(temp1);
                 count++;

                maze[i][j] = 3;
            


            }
        }
        // Checking the right direction 
        else if (d == 3)
        {
            if (j + 1 < SIZE && maze[i][j + 1] == 0 && visited[i][j + 1])
            {

                node temp1(i, j + 1);
                
                visited[i][j + 1] = false;



            foo = make_pair(i, j);
                


            
                
                s.push(temp1);
                 count++;

            
                maze[i][j] = 3;


            }
        }

        // If none of the direction leads to end point 
        // pop the value  from where it enter.
        else
        {
            visited[temp.x][temp.y] = true;


           foo = make_pair(i, j);

    

            
            
            s.pop();
            count--;

            maze[i][j] = 7;

    


        }
        

    }
    
    
    return count;
    
    return 0;
}

Result: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...