目前,我有一个迷宫解决算法,由于某种原因它进入无限循环,我已经使用了几个小时,但我似乎无法弄明白。难道我做错了什么。我将发布一些其他必要的功能,这些功能可能会与我的迷宫解决代码一起成为潜在的问题。
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mazegen.h"
#define MAXSIZE 100
int main(int argc, char**argv)
{
char*readFile;
char maze[MAXSIZE][MAXSIZE];
int rows=0;
int cols=0;
int x;
int y;
FILE*fp;
int counter;
int length=0;
counter = 0;
fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("Cannot open file\n");
exit(0);
}
readFile = malloc(sizeof(char)*MAXSIZE);
while(fgets(readFile,MAXSIZE,fp) != NULL)
{
for(cols=0; cols <MAXSIZE; cols++)
{
maze[rows][cols] = readFile[cols];
}
rows++;
counter++;
}
fclose(fp);
length = strlen(readFile);
mazeSolution(maze,counter, length);
for(x=0; x<rows; x++)
{
for(y=0; y<cols; y++)
{
printf("%c", maze[x][y]);
}
}
free(readFile);
return 0;
}
mazeSolve.c
#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();
/*push(currentCell, 1,2);
push(currentCell, 3,4);
pop(currentCell);
printStack(currentCell);*/
for(i=0; i<counter; i++)
{
for(j=0; j<counter2; j++)
{
if(maze[i][j] == 'S')
{
push(currentCell,i,j);
i = counter;
break;
}
}
}
while(currentCell != NULL)
{
if(maze[i][j] == 'F')
{
break;
}
if (maze[i][j] == ' ')
{
maze[i][j] = '.';
}
if (i>0 && maze[i-1][j] == ' ')
{
push(currentCell,i-1, j);
i--;
}
else if (j>0 && maze[i][j-1] == ' ')
{
push(currentCell,i, j-1);
j--;
}
else if (i+1<counter && maze[i+1][j] == ' ')
{
push(currentCell,i+1, j);
i++;
}
else if (j+1<counter2 && maze[i][j+1] == ' ')
{
push(currentCell,i, j+1);
j++;
}
else
{
pop(currentCell);
}
}
if (listLength(currentCell->list) > 0)
{
printStack(currentCell);
}
else
{
printf("NO SOLUTION\n");
}
}
stack.c
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
stack*create()
{
stack*myStack;
myStack = malloc(sizeof(stack)+1);
myStack->list = createList();
return myStack;
}
node*push(stack*theStack, int xpos, int ypos)
{
node*top;
if(theStack == NULL)
{
printf("Empty Stack.Error\n");
exit(0);
}
top = addFront(theStack->list, xpos, ypos);
return top;
}
void pop (stack*theStack)
{
node*theHead;
if(theStack == NULL)
{
printf("Empty Stack. Error\n");
exit(0);
}
theHead = removeFromFront(theStack->list);
theStack->list = theHead;
}
void peek (stack*theStack)
{
getFromFront(theStack->list);
}
void printStack(stack*theStack)
{
if(theStack == NULL)
{
printf("Error Empty Stack. Cannot print stack\n");
exit (1);
}
printList(theStack->list);
}
void destroyStack(stack*theStack)
{
destroyList(theStack->list);
free(theStack);
}
linkedList.c
#include "linkedList.h"
#include <stdio.h>
#include <stdlib.h>
node*createList()
{
node*dummyNode;
dummyNode = malloc(sizeof(node));
dummyNode->next = NULL;
return dummyNode;
}
node*addFront(node*list, int xpos, int ypos)
{
node*newNode;
if(list == NULL)
{
printf("Add to front Error\n");
exit(0);
}
newNode = initNode(xpos, ypos);
newNode->next = list->next;
list->next = newNode;
return list;
}
void printList(node*theList)
{
node*currentPosition;
currentPosition = theList->next;
while(currentPosition != NULL)
{
printf("This is the value of the current node : %d,%d\n",currentPosition->xpos, currentPosition->ypos);
currentPosition= currentPosition->next;
}
}
node*initNode(int xpos, int ypos)
{
node*newNode;
newNode = malloc(sizeof(node));
newNode->xpos = xpos;
newNode->ypos = ypos;
newNode->next = NULL;
return newNode;
}
void destroyList(node*theList)
{
node*temp;
while(theList->next != NULL)
{
temp = theList->next;
free(theList);
theList=temp;
}
}
node*removeFromFront(node*theList)
{
node*temp;
if (theList == NULL)
{
printf("Error\n");
return NULL;
}
temp = theList->next;
theList->next = NULL;
return temp;
}
int listLength(node*list)
{
int length;
node*ptr;
length = 0;
ptr = list->next;
while(ptr != NULL)
{
length++;
ptr = ptr->next;
}
printf("This is the length of the list : %d\n",length);
return length;
}
void getFromFront(node*theList)
{
node*temp;
int value1;
int value2;
temp = theList->next;
if(temp == NULL)
{
printf("Empty List.\n");
exit(0);
}
value1 = theList->next->xpos;
value2 = theList->next->ypos;
printf("This is the value from the front : %d,%d\n", value1, value2);
}
答案 0 :(得分:0)
看起来可能有几件事:你的while循环。你的while循环使用表达式
while (<insert code> != null)
{
<insert body of code>
}
使用!= null
表达式有时很棘手,因为除非条件为真,否则它将继续进入无限循环。我注意到在你的代码中,你的while循环中没有任何退出语句。根据我的经验,循环是无限循环的主要原因。似乎这种情况不会是错误的(= null
)。例如:
while(fgets(readFile,MAXSIZE,fp) != NULL)
{
for(cols=0; cols <MAXSIZE; cols++)
{
maze[rows][cols] = readFile[cols];
}
rows++;
counter++;
}
如果fgets
方法没有变为null,则永远不会退出while循环。你没有发布fgets
方法,所以我担心我不能说你问题所在的位置。第二个while循环也是如此。
我在调试无限循环时经常喜欢使用的一个好策略是在while循环中使用System.out.println
(我习惯于java haha)。这样做可以让我看到计算机在while循环中做了什么。