0

My task was to find if a path exists from source to destination in a given graph(adjacency matrix). The source is 1 and the destination is 2 and the path can travel only through number 3 in the matrix. I have used BFS to solve this problem but I am getting Double free or corruption error. If anyone could please help me, I am still a beginner in coding. I have attached the code here. The question is as follows Given a N X N matrix (M) filled with 1, 0, 2, 3. The task is to find whether there is a path possible from source to the destination while traversing through blank cells only. You can traverse up, down, right, and left.

A value of cell 1 means Source. A value of cell 2 means Destination. A value of cell 3 means Blank cell. A value of cell 0 means Blank Wall. Note: there are only a single source and a single destination. The first line of input is an integer T denoting the no of test cases. Then T test cases follow. Each test case consists of 2 lines. The first line of each test case contains an integer N denoting the size of the square matrix. Then in the next line is N*N space-separated values of the matrix (M)

    #include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

bool inside(int first, int second,int r)
{
    if(first<r && second<r && first>=0 && second>=0)
    {
        return true;
    }
    return false;
}
int isPath(pair <int, int> source, pair <int,int> dest, vector <vector<int>> &adj, vector <vector <bool>> &visit, int r)
{
    queue <pair<int,int>> q;
    cout<<"hello";
    q.push(source);
    while(!q.empty())
    {
        pair <int, int> curr = q.front();
        cout<<adj[curr.first][curr.second];
        q.pop();
        if(inside(curr.first,curr.second,r) && adj[curr.first][curr.second]==2)
        {
            return 1;
        }
        if(inside(curr.first,curr.second,r) && !visit[curr.first][curr.second] && adj[curr.first][curr.second]==1)
        {
            visit[curr.first][curr.second] = true;
        }
        if(inside(curr.first+1,curr.second,r) && (adj[curr.first+1][curr.second]==3||adj[curr.first+1][curr.second]==2) && !visit[curr.first+1][curr.second])
        {
            curr.first++;
            q.push(curr);
            visit[curr.first][curr.second]=true;
            curr.first--;
        }
        if(inside(curr.first-1,curr.second,r) && (adj[curr.first-1][curr.second]==3||adj[curr.first-1][curr.second]==2) && !visit[curr.first-1][curr.second])
        {
            curr.first--;
            q.push(curr);
            visit[curr.first][curr.second]=true;
            curr.first++;
        }
        if(inside(curr.first,curr.second+1,r) && (adj[curr.first][curr.second+1]==3||adj[curr.first][curr.second+1]==2) && !visit[curr.first][curr.second+1])
        {
            curr.second++;
            q.push(curr);
            visit[curr.first][curr.second]=true;
            curr.second--;
        }
        if(inside(curr.first,curr.second-1,r) && (adj[curr.first][curr.second-1]==3||adj[curr.first][curr.second-1]==2) && !visit[curr.first][curr.second-1])
        {
            curr.second--;
            q.push(curr);
            visit[curr.first][curr.second]=true;
            curr.second++;
        }

    }
    return 0;
}
int main() {
    int r,c,t,i,j;
    vector <vector <int>> adj;
    vector <vector <bool>> visit;
    cin>>t;
    int p=t;
    vector <int> store(t);
    pair <int,int> source;
    pair <int,int> dest;
    while(t--)
    {
        cin>>r;
        adj = vector <vector <int>>(r, vector<int> (r));
        visit = vector <vector <bool>>(r, vector<bool> (r));
        for(i=0;i<r;i++)
        {
            for(j=0;j<r;j++)
            {
                cin>>adj[i][j];
            }
        }
        for(i=0;i<r;i++)
        {
           for(j=0;j<r;j++)
            {
                visit[i][j]=false;
            }
        }
        for(i=0;i<r;i++)
        {
            for(j=0;j<r;j++)
            {
                if(adj[i][j]==1)
                {
                    source.first=i;
                    source.second=j;
                }
                if(adj[i][j]==2)
                {
                    dest.first=i;
                    dest.second=j;
                }
            }
        }
        int k=0, max=0;
        store[t-1]=isPath(source, dest, adj, visit,r);
    }
    for(i=0;i<p;i++)
    {
        cout<<store[i];
    }
    return 0;
}
  • 1
    1) What is the test data that fails? 2) Use `vector.at()` instead of `[ ]` to access your elements. More than likely you are going out-of-bounds, and `at()` will tell you where. – PaulMcKenzie Jun 19 '20 at 12:16
  • Attaching your entire code is inadvisable. You should construct a [mre] that demonstrates the issue you are facing. Often, this means *not* getting input from the user; just initialize your variables to the data for the problematic case (**one** test case, not `t` of them). Ideally, the [mre] is abstract enough that it is no longer helpful to quote the toy question you were given, abstract enough that answers might help someone facing the same core issue in a different situation. – JaMiT Jun 19 '20 at 16:21

0 Answers0