2014-06-09 29 views
-1

目前,我有一個迷宮求解算法,並由於某種原因進入了無限循環,我已經上了好幾個小時,但我似乎無法形象出來。難道我做錯了什麼。我將發佈一些其他必要的功能,這些功能可能是我的迷宮解決代碼的潛在問題。我的解決迷宮的代碼進入了一個無限循環

的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

看起來它可能是幾件事情:你的while循環。您while循環使用表達式

while (<insert code> != null) 
{ 
<insert body of code> 
} 

使用!= null表達是棘手的時間,因爲除非條件爲真,這將繼續在一個無限循環下去。我注意到在你的代碼中,你的while循環中沒有任何類型的exit語句。根據我的經驗,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哈哈)。這樣做可以讓我看到計算機在while循環內正在做什麼。

+0

哦好吧,我從來沒有做過java實際上是system.out.println在while循環中的printf語句?我沒有發佈fgets方法嗎? :S當處理while循環時,你認爲什麼是一個好的退出條件?一種方法是直到文件末尾讀取,而不是NULL – user3712556

+0

'System.out.println'只是在屏幕上打印出來,是的,它會在while循環中。如果我錯過了你的fgets方法,我很抱歉,你的代碼長,我習慣以eclipse或BlueJ格式閱讀代碼,而不是SO。至於while循環,通常我會盡量避免它們。嘗試設置更具體的條件,而不是像'!= null'那樣含糊不清。這就是爲什麼我發現循環是一個更好的選擇,他們更具體,並不傾向於容易地進入無限循環。 – user3695782

+0

那麼我嘗試了一些特定的東西,雖然它不等於'F',因爲那是我們迷宮中位置的結束,但它似乎不是我可能必須嘗試在while循環中放置一堆printf語句的工作像你說的。感謝您的幫助,我真的很感激。 – user3712556