简体   繁体   English

无法使用 BACKTRACING 找出分段错误 SIGSEGV 查找路径问题的原因

[英]cant figure out the reason for segmentation fault SIGSEGV-finding path problem using BACKTRACING

I need to develop and implement the algorithm that will produce the path existing in between the first pixel and the last pixel.我需要开发和实现将产生存在于第一个像素和最后一个像素之间的路径的算法。

INPUT:line 1 the size of the grid is M×N. INPUT:第 1 行网格的大小为 M×N。 line 2:space separated 5 numbers.第 2 行:空格分隔 5 个数字。 The first is an integer, say i, representing a pixel number followed by a sequence of four bits (0/1) showing the directions one can move from pixel i.第一个是 integer,比如 i,表示像素编号,后跟四位 (0/1) 的序列,显示可以从像素 i 移动的方向。

Sample Input:样本输入:

5 8
1 0 1 1 0
2 0 1 1 1
3 0 1 0 1
4 0 0 1 1
5 0 1 1 0
6 0 1 1 1
7 0 1 0 1
8 0 0 1 1
9 1 1 1 0
10 1 0 0 1
11 0 1 0 0
12 1 0 1 1
13 1 0 1 0
14 1 1 0 0
15 0 0 1 1
16 1 0 1 0
17 1 0 1 0
18 0 1 1 0
19 0 1 0 1
20 1 0 1 1
21 1 0 1 0
22 0 1 1 0
23 1 0 0 1
24 1 0 0 0
25 1 0 1 0
26 1 1 0 0
27 0 0 1 1
28 1 0 1 0
29 1 0 1 0
30 1 0 1 0
31 0 1 1 0
32 0 0 1 1
33 1 1 0 0
34 0 0 0 1
35 1 0 0 0
36 1 1 0 0
37 1 0 0 1
38 1 1 0 0
39 0 1 0 1
40 1 0 0 1

Output:display the numbers traversed in the path.(RIGHT NOW I AM JUST PRINTING THE PATHS REPRESENTING 1 INFORM OF MATRIX(sol)) Output:显示路径中遍历的数字。(现在我只是打印代表 1 INFORM OF MATRIX(sol)的路径)

I am facing segmentation error at this line if (isSafe(t,x,y)==true && solveMazeUtil( t,r,b,l, x - 1, y, sol) == true) and i think there is problem with logic. if (isSafe(t,x,y)==true && solveMazeUtil( t,r,b,l, x - 1, y, sol) == true)我在这一行遇到分段错误,我认为有问题有逻辑。 THANKS IN ADVANCE!!提前致谢!! sample pixel image样本像素图像

#include <bits/stdc++.h>

#define max 1000
using namespace std;
// Maze size 
int m, n;

bool solveMazeUtil(int t[], int r[], int b[], int l[], int x, int y, int ** sol);

void printSolution(int ** sol) {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cout << sol[i][j];
    cout << "\n";
  }
}

/* A utility function to check if x, 
y is valid directions */
bool isSafe(int check[], int x, int y) {
  // if (x, y outside maze) return false 
  if (x < 0 || x >= m) return false;
  if (y < 0 || y >= n) return false;
  int ind = x * n + y;
  return (check[ind] == 1);

}

/* This function solves the Maze problem 
using Backtracking. It mainly uses 
solveMazeUtil() to solve the problem. 
It returns false if no path is possible, 
otherwise return true and prints the path 
in the form of 1s. */
bool solveMaze(int t[], int r[], int b[], int l[]) {
  int ** sol;
  sol = new int * [m];
  for (int i = 0; i < n; i++)
    sol[i] = new int[n];
  std::fill(sol[0], sol[0] + m * n, 0);

  if (solveMazeUtil(t, r, b, l, 0, 0, sol) == true) {
    printSolution(sol);
    return true;
  }

  return false;
}

/* A recursive utility function 
 */
bool solveMazeUtil(int t[], int r[], int b[], int l[], int x, int y, int ** sol) {
  sol[x][y] = 1;
  // if (x, y is goal) return true 
  if (x == m - 1 && y == n - 1) {
    return true;
  }
  if (isSafe(t, x, y) == true && solveMazeUtil(t, r, b, l, x - 1, y, sol) == true)
    return true;
  if (isSafe(r, x, y) == true && solveMazeUtil(t, r, b, l, x + 1, y, sol) == true)
    return true;
  if (isSafe(b, x, y) == true && solveMazeUtil(t, r, b, l, x, y + 1, sol) == true)
    return true;
  if (isSafe(l, x, y) == true && solveMazeUtil(t, r, b, l, x, y - 1, sol) == true)
    return true;
  /* If none of the above movements 
  work then BACKTRACK: unmark 
  x, y as part of solution path */
  sol[x][y] = 0;
  return false;

  return false;
}

// driver program to test above function 
int main() {
  int t[max], r[max], l[max], b[max], v[max];
  cin >> m >> n;
  for (int i = 0; i < m * n; i++) {
    cin >> v[i] >> t[i] >> r[i] >> b[i] >> l[i];
  }
  solveMaze(t, r, b, l);
  return 0;
}

It is very important to write readible code.编写可读的代码非常重要。 It will help you make less mistakes and will also make it easier to maintain the code in the future.它将帮助您减少错误,并且还将使将来更容易维护代码。 An important aspect is variable naming.一个重要的方面是变量命名。 I had to think about it myself.我不得不自己考虑。 but t , r , b , l probably denote "top" "right" "bottom" and "left.但是trbl可能表示“顶部”“右侧”“底部”和“左侧”。

Now let me rewrite a part of your code (leaving out the not properly encapsulated stuff):现在让我重写您的部分代码(省略未正确封装的内容):

    if ((isSafe(top)    && solveMazeUtil(x - 1, y)) ||
        (isSafe(right)  && solveMazeUtil(x + 1, y)) ||
        (isSafe(bottom) && solveMazeUtil(x, y + 1)) ||
        (isSafe(left)   && solveMazeUtil(x, y - 1))) return true;

Do you see the problem?你看到问题了吗?

edit: I also see that you've got your indexing wrong.编辑:我还看到您的索引错误。 Numbering in the cells goes like:单元格中的编号如下所示:

 1  2  3  4  5  6  7  8
 9 10 11 12 13 14 15 16
17 18 19 20 ...

thus the index in the arrays need to be x + y*N .因此 arrays 中的索引需要为x + y*N

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

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