如何清除C ++代码中的SIGABRT错误 [英] How to remove SIGABRT error in my C++ code

查看:90
本文介绍了如何清除C ++代码中的SIGABRT错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是查找给定图形(邻接矩阵)中从源到目标的路径是否存在。源为1,目标为2,路径只能通过矩阵中的3。我已经使用BFS解决了此问题,但出现了SIGABRT错误。如果有人可以帮助我,我仍然是编码的初学者。我在这里附上了代码。
问题如下:给定一个用1,0,2,3填充的N X N矩阵(M)。任务是查找仅遍历空白单元格时是否存在从源到目标的路径。您可以上下左右移动。

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.

单元格1的值表示源。
单元格2的值表示目标。
单元格3的值表示空白单元格。
单元格0的值表示空白墙。
注意:只有一个来源和一个目的地。
输入的第一行是一个整数T,表示测试用例的数量。然后是T测试用例。每个测试用例包含2行。每个测试用例的第一行包含一个整数N,它表示方阵的大小。然后在下一行中是矩阵(M)的N * N个空格分隔的值。

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;
}


推荐答案

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

您使用 r 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));

您正在使用 r 和<$ c遍历向量时,对于的$ c> r 循环限制。您是说 r c 吗?

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

这篇关于如何清除C ++代码中的SIGABRT错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆