簡體   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