繁体   English   中英

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

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

我需要开发和实现将产生存在于第一个像素和最后一个像素之间的路径的算法。

INPUT:第 1 行网格的大小为 M×N。 第 2 行:空格分隔 5 个数字。 第一个是 integer,比如 i,表示像素编号,后跟四位 (0/1) 的序列,显示可以从像素 i 移动的方向。

样本输入:

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:显示路径中遍历的数字。(现在我只是打印代表 1 INFORM OF MATRIX(sol)的路径)

if (isSafe(t,x,y)==true && solveMazeUtil( t,r,b,l, x - 1, y, sol) == true)我在这一行遇到分段错误,我认为有问题有逻辑。 提前致谢!! 样本像素图像

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

编写可读的代码非常重要。 它将帮助您减少错误,并且还将使将来更容易维护代码。 一个重要的方面是变量命名。 我不得不自己考虑。 但是trbl可能表示“顶部”“右侧”“底部”和“左侧”。

现在让我重写您的部分代码(省略未正确封装的内容):

    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;

你看到问题了吗?

编辑:我还看到您的索引错误。 单元格中的编号如下所示:

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

因此 arrays 中的索引需要为x + y*N

暂无
暂无

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

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