[英]Segfaulting while finding path through maze
I am trying to print out the coordinates of the maze as I am trying to solve using depth first search algorithm, however, it prints out the initial position, but it seg faults. 在尝试使用深度优先搜索算法进行求解时,我试图打印出迷宫的坐标,但是,它会打印出初始位置,但存在段错误。 Is there something I am doing wrong??? 我做错什么了吗??? Here is my code : 这是我的代码:
#include "mazegen.h"
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUFFERSIZE 500
#define FLAG
void mazeSolution(char maze[100][100], int counter, int counter2)
{
stack*currentCell;
int i;
int j;
currentCell = create();
for(i=0; i<counter; i++)
{
for(j=0; j<counter2; j++)
{
if(maze[i][j] == 'S')
{
push(currentCell,i,j);
currentCell->visited = true;
}
}
}
printStack(currentCell);
while(currentCell != NULL)
{
pop(currentCell);
if(maze[i][j] == 'F')
{
break;
}
if(i != 0)
{
if(maze[i-1][j] == ' ' && currentCell->visited != true)
{
currentCell->visited = true;
push(currentCell,i-1,j);
}
}
if(maze[i+1][j] == ' ' && currentCell->visited != true)
{
currentCell->visited = true;
push(currentCell,i+1,j);
}
if(j != 0)
{
if(maze[i][j-1] == ' ' && currentCell->visited != true)
{
currentCell->visited = true;
push(currentCell, i,j-1);
}
}
if(maze[i][j+1] == ' ' && currentCell->visited != true)
{
currentCell->visited = true;
push(currentCell, i, j+1);
}
}
printf("No solution\n");
printStack(currentCell);
}
I think it has something to do with my pop function and with the way I have implemented it 我认为它与我的pop函数以及实现它的方式有关
void pop (stack*theStack)
{
node*theHead;
if(theStack == NULL)
{
printf("Empty Stack. Error\n");
exit(0);
}
theHead = removeFromFront(theStack->list);
theStack->list = theHead;
}
node*removeFromFront(node*theList)
{
node*temp;
temp = theList->next;
if(temp == NULL)
{
printf("pop Error\n");
return NULL;
}
theList = theList->next;
return theList;
}.
Look at this portion of your code: 看一下这段代码:
while(currentCell != NULL)
{
pop(currentCell);
if(maze[i][j] == 'F')
{
break;
}
...
}
You are popping a value from the stack - this is ok. 您正在从堆栈中弹出一个值-可以。 But you need to assign the popped value to i
and j
. 但是您需要将弹出的值分配给i
和j
。 In your code, the value of i
and j
remain unchanged, so you have an infinite while
loop. 在您的代码中, i
和j
的值保持不变,因此您有一个无限的while
循环。 You are continuously pushing values in the stack inside this loop, which ultimately leads to segfault. 您在此循环内不断将值推入堆栈中,最终导致段错误。
Keeping a visited status on the stack is useless - you need to check if the specific position has been visited when you try to access it from some new direction, which may happen if there is a loop in your maze. 在堆栈上保持已访问状态是没有用的-当您尝试从某个新方向访问特定位置时,您需要检查是否已访问了特定位置,如果迷宫中有一个循环,则可能会发生。 So the proper place for this attribute is a maze itself, not a stack. 因此,此属性的适当位置是迷宫本身,而不是堆栈。
You don't need separate stack
and node
structures - the stack is actually a list of nodes. 您不需要单独的stack
和node
结构-堆栈实际上是节点列表。
Building a stack is quite easy, but you need to imagine it's structure and behaviour before you start encoding. 构建堆栈非常容易,但是在开始编码之前 ,您需要想象它的结构和行为。 There are two basic (plain C) solutions: use a pre-allocated array or dynamically linked list. 有两种基本(普通C)解决方案:使用预分配的数组或动态链接列表。 Let's use an array. 让我们使用一个数组。
A stack's item will be 堆叠的物品将是
typedef struct node {
int i, j;
} Node;
a stack itself: 堆栈本身:
Node stack[ 10001];
and a number of items currently stacked: 和当前堆叠的一些物品:
int stkptr = 0;
Now we can test if the stack contains any data: 现在我们可以测试堆栈中是否包含任何数据:
int StackNonEmpty() { return stkptr; }
push new position onto the stack: 将新位置推入堆栈:
void Push(int i, int j) {
stack[ stkptr].i = i;
stack[ stkptr].j = j;
stkptr ++;
}
read a position from the stack's top (assuming StackNonEmpty() != 0
): 从堆栈的顶部读取一个位置(假设StackNonEmpty() != 0
):
void Fetch(int *pi, int *pj) {
*pi = stack[ stkptr - 1].i;
*pj = stack[ stkptr - 1].j;
}
and remove it from the stack: 并将其从堆栈中删除:
void Pop() { stkptr --; }
We also need to define a marker for visited positions: 我们还需要为访问位置定义标记:
int VISITED = '.';
or 要么
#define VISITED '.'
and we're ready to write the algorithm: 现在我们准备编写算法:
for(i=0; i<counter; i++)
for(j=0; j<counter2; j++)
if(maze[i][j] == 'S')
{
Push(i,j); // set the Start position
i = counter; // this is to break the outer loop
break; // and this breaks the inner loop
}
while( StackNonEmpty())
{
Fetch(& i, & j); // get a current position
if( maze[i,j] == 'F') // Finish found?
break;
if( maze[i,j] == ' ') // empty cell?
maze[i,j] = VISITED;
// find next possible step
if( i > 0 && maze[i-1,j] == ' ') // cell to the left is empty
Push(i-1,j); // step left
else
if( j > 0 && maze[i,j-1] == ' ') // cell above is empty
Push(i,j-1); // step up
else
if( i+1 < counter && maze[i+1,j] == ' ')
Push(i+1,j); // step right
else
if( j+1 < counter2 && maze[i,j+1] == ' ')
Push(i,j+1); // step down
else // dead end - no way out
Pop(); // step back
}
if( StackNonEmpty()) // exited with break, so 'F' found
PrintStack();
else
PrintFailureMessage(); // could not reach 'F'
To show the results just print all items of the stack
array until the stkptr
position: 要显示结果,只需打印stack
数组的所有项目,直到stkptr
位置:
void PrintStack() {
for(i = 0; i < stkptr; i ++)
printf( "%d,%d\n", stack[i].i, stack[i].j);
}
EDIT 编辑
'VALID' status assignment corrected (was == instead of =), PrintFailureError
replaced with PrintFailureMessage
. 纠正了“有效”状态分配(是==而不是=),将PrintFailureError
替换为PrintFailureMessage
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.