0

我正在编写一个解决迷宫的程序。它从起始方“o”开始,到“*”结束。“#”是墙壁和“。” 是可以探索的方块。该算法是在程序开始时将开始方块添加到一个空队列中。之后的每个后续步骤,您从队列中取出一个方格并检查它是否是已记录的方格、是否是完成方格或是否可探索。如果可以探索,则将相邻的非墙方块添加到队列中。我的问题是,当我将起始方块添加到队列中时,我遇到了分段错误,我不知道为什么。下面是相关代码:

mazeQ.c:

#include <stdio.h>
#include <stdlib.h>
#include "QueueInterface.h"

int main(void)
{
    Queue Q;
    InitializeQueue(&Q);

    int row;
    int column;

    int x;
    int y;

    scanf("%d %d", &column, &row); //scan dimensions of maze

    char input[row][column];
    ItemType maze[row][column];
    for(x = 0; x <= row; x++) {
            fgets(input[x],column+2,stdin);
    }
    row++;

    for (x = 1; x < row; x++){
            for (y = 0; y < column; y++) {
                    maze[x][y].squareType = input[x][y];
                    maze[x][y].xCoordinate = x-1;
                    maze[x][y].yCoordinate = y;
                    maze[x][y].recordedSquare = 0;
            }
    }

    for(x = 1; x < row; x++){
            for(y = 0; y < column; y++) {
                    if (maze[x][y].squareType == 'o')
                      Insert(maze[x][y],&Q);  //INSERTED HERE
            }
    }
    for (x = 1; x < row; x++) {
            for (y = 0; y < column; y++) {
                    printf("%c", maze[x][y].squareType);
            }
    printf("\n");
    }

}

用户类型.h:

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    char squareType;
    int xCoordinate;
    int yCoordinate;
    int recordedSquare;
} ItemType;

下面的两个文件必须保持原样。QueueImplementation.c:

#include <stdio.h>                   
#include <stdlib.h>                 
#include "QueueInterface.h"         


void SystemError(char *errorMsg) {fprintf(stderr,errorMsg);}

void InitializeQueue(Queue *Q)
{
  Q->Front = NULL;
  Q->Rear  = NULL;
}

int Empty(Queue *Q)
{
  return (Q->Front == NULL);
}

int Insert(ItemType R, Queue *Q)
{   
  QueueNode *Temp;
                                               /* attempt to allocate */
  Temp = (QueueNode *) malloc(sizeof(QueueNode));       /* a new node */

  if (Temp == NULL) {               /* Temp = NULL signals allocation */
     SystemError("system storage is exhausted");           /* failure */
     return 0;
  } else {
     Temp->Item = R;
     Temp->Link = NULL;
     if ( Q->Rear == NULL ) {
        Q->Front = Temp;
        Q->Rear = Temp;
     } else {
        Q->Rear->Link = Temp;
        Q->Rear = Temp;
     }
   }
   return 1;
}

队列接口.h:

#include "UserTypes.h"                          /* get ItemType definition */

typedef  struct  QueueNodeTag {
        ItemType               Item;   
        struct  QueueNodeTag  *Link;
     }QueueNode;

typedef  struct {                                 /* a queue is empty iff */
        QueueNode  *Front;                       /* its Front == NULL */
        QueueNode  *Rear;
     }Queue;


/* defined operations */

extern void InitializeQueue(Queue *Q);
  /* Initialize the queue Q to be the empty queue */

extern int Empty(Queue *Q);
  /* Returns TRUE == 1 if and only if the queue Q is empty */

extern int Full(Queue *Q);
  /* Returns TRUE == 1 if and only if the queue Q is full */

extern int Insert(ItemType R, Queue *Q);
  /* If Q is not full, insert a new item R onto the rear of Q */

extern int Remove(Queue *Q, ItemType *F);
  /* If Q is non-empty, remove the frontmost item of Q and put it in F */

该程序通过键入 mazeQ < sampleMaze 运行

样品迷宫

12 10
############
#..........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#
############
4

2 回答 2

1

我不知道你为什么会得到它的段错误。我通过运行它得到了这个输出:

$ gcc *.c -o maze
$ ./maze <sample 
ļ##
ļ####
ļ######
ļ########
##########
############
###########.
#########...
#######.....
#####......#

好的,经过一些清理后,我得到了以下代码:

#include <stdio.h>
#include <stdlib.h>
#include "QueueInterface.h"

int main(void)
{
    Queue Q;
    InitializeQueue(&Q);

    int row;
    int column;

    int x;
    int y;

    scanf("%d %d\n", &column, &row); //scan dimensions of maze


    char input[row][column+2];
    ItemType maze[row][column];
    for(x = 0; x < row; x++) {
            fgets(input[x],column+2,stdin);
    }

    for (x = 0; x < row; x++){
            for (y = 0; y < column; y++) {
                    maze[x][y].squareType = input[x][y];
                    maze[x][y].xCoordinate = x;
                    maze[x][y].yCoordinate = y;
                    maze[x][y].recordedSquare = 0;
            }
    }

    for(x = 0; x < row; x++){
            for(y = 0; y < column; y++) {
                    if (maze[x][y].squareType == 'o')
                      Insert(maze[x][y],&Q);  //INSERTED HERE
            }
    }
    for (x = 0; x < row; x++) {
            for (y = 0; y < column; y++) {
                    printf("%c", maze[x][y].squareType);
            }
    printf("\n");
    }

}

它的输出:

############
#..........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#
############

试试看。

于 2012-10-14T04:41:24.460 回答
0

显然问题是我放置程序文件的目录有太多其他垃圾文件占用内存。我所要做的就是将我的程序文件移动到另一个目录。我想我吸取了教训,总是为新程序创建一个新目录。感谢大家的帮助。

于 2012-10-14T12:18:58.223 回答