简体   繁体   English

查找迷宫C ++中是否存在路径

[英]Find if path exist or not in maze c++

My code is giving me runtime error. 我的代码给了我运行时错误。 I can't figure it out how to resolve it? 我不知道该如何解决? It's not even working for smaller matrix 4 X 4 . 它甚至不适用于较小的矩阵4 X 4。 Matrix size for the problem is not more than 20 x 20. 该问题的矩阵大小不超过20 x 20。

Code: 码:

#include <iostream>
using namespace std;
int a[20][20];
bool findpath(int ar[][20],int i,int j,int size)
{
    if (ar[i][j]==0 || i>(size-1) || j>(size-1) || i<0 || j<0)
        return false;
    if (ar[i][j]==2)
        return true;
    if ((findpath(ar,i+1,j,size)) || (findpath(ar,i,j+1,size)) 
    || (findpath(ar,i-1,j,size)) || (findpath(ar,i,j-1,size)))
        return true;
    return false;
}
int main() {
    int t;
    cin>>t;
    while(t--)
    {   int n;
        cin>>n;

        int r,c;

        //size = n;
        for(int i =0 ;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>a[i][j];
                if (a[i][j]==1)
                   { r=i;
                    c=j;
                   }
            }

        }
        //cout<<r<<c;
        bool b = findpath(a,r,c,n);
        if (b)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;

    }
    return 0;
}

Input: 输入:

1
4
3 0 0 0 0 3 3 0 0 1 0 3 0 2 3 3 

Output: 输出:

YES

But I am getting Segmentation Fault (SIGSEGV) 但是我遇到了Segmentation Fault (SIGSEGV)

Check the order of evaluation of your statement if (ar[i][j]==0 || i>(size-1) || j>(size-1) || i<0 || j<0) . if (ar[i][j]==0 || i>(size-1) || j>(size-1) || i<0 || j<0) ,请检查语句的评估顺序。 You will access ar[i][j] to evaluate the first expression even if i is out of bounds or j is out of bounds. 即使i超出范围或j超出范围,您也将访问ar [i] [j]来评估第一个表达式。 It should be in the order so that when a short circuit does happen in the if condition you are safe/does not result in undefined behaviour for example: 它的顺序应该这样,以便在if条件下发生短路确实是安全的/不会导致不确定的行为,例如:

if (i < 0 || i >= size || j < 0 || j >= size || ar[i][j]==0) . if (i < 0 || i >= size || j < 0 || j >= size || ar[i][j]==0) Now if i < 0 it shorcircuits and does not need to check the rest and does not evaluate ar[i][j] . 现在,如果i < 0它将停滞不前,无需检查其余部分,并且不评估ar[i][j]

As you mentioned this is not working, here is a working version which I will explain. 正如您提到的那样,这是行不通的,下面是一个有效的版本,我将向您解释。 First I have changed your C style arrays to vectors and rather I use those to get row and col sizes. 首先,我将您的C样式数组更改为向量,而是使用它们来获取行和列的大小。 I also removed your inputs from users which you can add in later and helps keep the problem simple. 我还从用户中删除了您的输入,您可以在以后添加这些输入,并有助于使问题保持​​简单。

#include <vector>
bool findpath(vector<vector<int>>ar,int i,int j,vector<vector<int>>& visited)
{

    if (i < 0 || i >= ar.size() || j < 0 || j >= ar[0].size() || ar[i][j] == 0 || visited[i][j]) return false;

    if (ar[i][j]==2) return true;

    visited[i][j] = true;
    if(findpath(ar,i+1,j,visited) || findpath(ar,i,j+1,visited) || findpath(ar,i-1,j,visited) || findpath(ar,i,j-1,visited)) return true;
    visited[i][j] = false;

    return false;
}
int main() {
        const int rows = 3;
        const int cols = 3;
        vector<vector<int>> arr = {{ 0 , 3 , 2 },
                                   { 3 , 3 , 0 },
                                   { 1 , 3 , 0 }};

        vector<vector<int>> visited(rows,vector<int>(cols,false));
        bool b = findpath(arr,1,1,visited);
        if (b)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;


    return 0;
}

In the main function I have just used a vector<vector<int>> to describe a maze which was in the link you posted. 在主要功能中,我只是使用vector<vector<int>>来描述迷宫,该迷宫位于您发布的链接中。 The i and j are the starting points in the example below that is 1 and 1 . ij是下面示例中的起点,即11 There is also a visited 2D array same as the maze. 还有一个与迷宫相同的visited二维数组。 This stops you from going in an infinite recursion by marking the spots you have already covered and if they dont work out you set vector[i][j] = false to backtrack. 通过标记已经覆盖的斑点,可以阻止您进行无限递归;如果无法解决,则可以将vector[i][j] = false为可回溯。 Lastly if any of your arrangements returns a valid result we return else we just return false. 最后,如果您的安排中的任何一个返回有效结果,我们将返回否则返回false。

You can view this demo live here 您可以在此处现场观看此演示

You also mention that 1 is the starting point. 您还提到1是起点。 In the example I have already started from 1 . 在示例中,我已经从1开始。 You can add a loop in main to first figure out the coordinates for the starting point. 您可以在main中添加一个循环,以便首先找出起点的坐标。 Again this should be enough to get you going. 同样,这应该足以使您前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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