How to remove SIGABRT error in my C++ code

≡放荡痞女 提交于 2020-07-09 12:51:08

问题


My task was to find if a path exist 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 SIGABRT 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 are 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;
    q.push(source);
    while(!q.empty())
    {
        pair <int, int> curr = q.front();
        q.pop();
        if(inside(curr.first,curr.second,r) && !visit[curr.first][curr.second] && adj[curr.first][curr.second]==2)
        {
            return 1;
        }
        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(r, vector <int>(c));
    vector <vector <bool>> visit(r, vector <bool>(c));
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            visit[i][j]=false;
        }
    }
    cin>>t;
    int p=t;
    vector <int> store(t);
    pair <int,int> source;
    pair <int,int> dest;
    while(t--)
    {
        cin>>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++)
            {
                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:


int r,c,t,i,j;
vector <vector <int>> adj(r, vector <int>(c));
vector <vector <bool>> visit(r, vector <bool>(c));

You use r and c before you initialize them. You should first read values, then initialize the vectors. For example:

int r,c,t,i,j;
// Declare vectors but don't initialize yet
vector <vector <int>> adj;
vector <vector <bool>> visit;

// Later
cin >> r;
cin >> c;

adj = vector <vector <int>>(r, vector <int>(c));
visit = vector <vector <bool>>(r, vector <bool>(c));

You are using r and r as for loop limits when you go through the vectors. Did you mean r and c?




回答2:


You have to initialize r and c. In this case r and c can be very big.



来源:https://stackoverflow.com/questions/62445325/how-to-remove-sigabrt-error-in-my-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!