简体   繁体   中英

Matrix Traversal to print all and shortest path - infinite loop

#include<iostream>
#include<random>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;

int path_checker(vector<pair<int,int> > path, int i, int j)
{
    //cout<<"path_checker"<<endl;
    std::vector<pair<int, int> >::iterator it;
    for(it = path.begin(); it!=path.end();it++)
        if(it->first == i && it->second ==j)
            return 1;
    return 0;
}

int isVertex(int i, int j, int n)
{
    //cout<<"isVertex"<<endl;
    if((i>=0) && (j>=0))
    {
        if((i <= n) && (j <= n))
            return 1;
    }
    return 0;
}


void printAllPathsU(int *array, int i, int j, int n, vector<pair<int,int> >     path,int index)
{
   // cout<<"PrintAllPathsU_first"<<endl;
   // vector<pair<int,int>> path2 = {0};
    if((i == n) && (j == n))
    {
        if((path_checker(path,i,j)))
        {
        cout<<"Inside printing path"<<endl;
        //vector<pair<int,int> >::iterator it;
        for(int i = 0; i < path.size();i++)
            cout<<"a["<<path[i].first<<"]["<<path[i].second<<"] ";
        return;
        }
        else
        {
            path.push_back(make_pair(i,j));
            cout<<"Inside printing path"<<endl;
        //vector<pair<int,int> >::iterator it;
            for(int i = 0; i < path.size();i++)
                cout<<"a["<<path[i].first<<"]["<<path[i].second<<"] ";
            return;
        }
    }
if((*((array+i*n)+j) == 1) && (!path_checker(path,i,j)))
{
    //cout<<path.size()<<endl;
    path.push_back(make_pair(i,j));
    index++;
    //len++;
    if(isVertex(i,j-1,n))
        printAllPathsU((int *)array,i,j-1,n,path,index);
    if(isVertex(i-1,j,n))
        printAllPathsU((int *)array,i-1,j,n,path,index);
    if(isVertex(i,j+1,n))
        printAllPathsU((int *)array,i,j+1,n,path,index);
    if(isVertex(i+1,j,n))
        printAllPathsU((int *)array,i+1,j,n,path,index);
}
else if((*((array+i*n)+j) == 1) && (path_checker(path,i,j)))
{
    //cout<<"inside second else"<<endl;
    return;
}
else if(*((array+i*n)+j) == 0)
{
   // cout<<"inside third else"<<endl;
    return;
}
}

    void printAllPaths(int *array, int n)
{
vector<pair<int,int> > path;
//cout<<"PrintALLPaths"<<endl;
printAllPathsU(array, 0, 0, n, path, 0);
}




int main()
{       //populating matrix
    int n;
    cout << "Enter value of n (for n x n matrix): ";
    cin >> n;
    int i;
    int j;
    int k;
    int test=1;
    int array[n][n];
    int randomval;
    int total_elements = n*n;
    int counter_0 = 0;
    int max_0 = 0.2 * total_elements;
    cout << "Number of zeros(20% of n*n) in matrix: ";
    cout<<max_0<<endl;
    int count=0;
    srand(time(0));
    for(i = 0;i < n;i++)
    {
        for(j = 0;j<n;j++)
        {
            array[i][j]=-1;
        }
    }
    while(count < total_elements)
        {
            i=rand()%n;
            j=rand()%n;
            if(array[i][j]==1 || array[i][j]==0)
            {
                continue;
            }
            else if(array[i][j] == -1)
            {   
                count+=1;
                if(i==0 && j==0)
                {
                    array[i][j]=1;
                }
                else if (counter_0 < max_0)
                {
                    counter_0+=1;
                    array[i][j] = 0;
                }
                else if(counter_0 >= max_0)
                {   
                    test+=1;
                    array[i][j] = 1;

                }

            }
            else{continue;}
        }
    cout<<"# of 1s:"<<test<<" & # of 0s:"<<counter_0<<endl;
    cout<<"Elements Populated:"<<count<<endl;
    cout<<"Total Elements in matrix:"<<total_elements<<endl;
    if(counter_0 < max_0)
    {   cout<<"adding more zeros"<<endl;
        while(k < (max_0 - counter_0))
        {
            i = rand()%n;
            j = rand()%n;
            if(array[i][j] == 0)
            {

            }
            else
            {
            array[i][j] = 0;
            k+=1;
            }
        }
    }
    for(i = 0;i < n;i++)
    {
        for(j = 0;j<n;j++)
        {
            cout<<array[i][j]<<" ";
        }
        cout<<endl;
    }


//printing paths
if(array[1][0]==0 && array[0][1]==0)
{
    cout<<"No Possible paths homie #snorlaxiseverywhere";
}else
{
//printing paths
    printAllPaths((int *)array, n);
}
return 0;
}

The question is to traverse through an * n matrix populated with 1s and 0s, with the number of 0s in the matrix being 20% of the total number of elements, and print all paths and then the shortest path from the [0][0] to [n][n], using four direction: up,down,left and right. So far I have tried to implement printing all possible paths. However, I am stuck in an infinite loop in the

else if((*((array+i*n)+j) == 1) && (!path_checker(path,i,j)))
{
...
}

I cout the path.size() to check what the size is becoming in each instance, and the output is somewhat like :

35
48
37
...and so on infinitely (values ranging approx between 30-50)

Any ideas how to correct this?

EDIT : Changed up some logic, not stuck infinite loop. But all of the function calls exit through the "second else" and the "third else" inside the function - printAllPathsU(...).

Thanks!

As long as the question is how to "print all paths", you should have an infinite loop because there are infinitely many paths. If the question is how to print all non-self-intersecting paths, then this rephrasing gives a hint on how to go about solving the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM