2014-06-09 2 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

보인다. 귀하의 동안 루프는 != null 표현이 조건이 참 않는 때문에,이 무한 루프에가는 것입니다 항상 까다 롭습니다 사용하여 표현

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

를 사용합니다. 귀하의 코드에서 while 루프에 exit 문을 사용하지 않았다는 것을 알았습니다. 내 경험에 루프 동안 무한 루프의 주요 원인입니다. 그 상태가 거짓일 것 같지 않습니다 (= null). 예를 들면 다음과 같습니다 fgets 방법은 널 (null)이되지 않을 경우

while(fgets(readFile,MAXSIZE,fp) != NULL) 
{ 
    for(cols=0; cols <MAXSIZE; cols++) 
    { 
     maze[rows][cols] = readFile[cols]; 
    } 
    rows++; 

    counter++; 
} 

, 당신은 while 루프를 종료하지 않습니다. fgets 메소드를 게시하지 않았으므로 문제가 어디에 있는지 여부를 말할 수는 없습니다. 두 번째 while 루프에도 같은 개념이 적용됩니다.

무한 루프를 디버깅 할 때 자주 사용하는 좋은 전략은 while 루프 내부에서 System.out.println (저는 java haha에 익숙합니다)을 사용하고 있습니다. 이렇게하면 while 루프 내에서 컴퓨터가 무엇을하는지 볼 수 있습니다.

+0

오, 좋아, 나는 실제로 java를 한 적이 없다는 사실은 system.out.println while 루프 내에서 printf statment입니까 ?? 내가 fgets 메소드를 게시하지 않았습니까? : S while 루프를 다룰 때 좋은 종료 조건을 제안합니까? 한 가지 방법은 NULL 대신 NULL을 추측하는 파일의 끝까지 읽는 것입니다. – user3712556

+0

'System.out.println'은 화면에 출력되고 네, while 루프 안에있을 것입니다. 죄송합니다. fgets 메서드를 놓친 경우 코드가 더 길어지고 Eclipse 또는 BlueJ 형식의 코드를 읽는 데 익숙해졌습니다. while 루프는 일반적으로 정직하게 노력하고 피한다. '! = null'과 같이 모호한 것 대신에 더 구체적인 조건을 설정해보십시오. 이것이 제가 루프를 더 나은 옵션으로 찾은 이유입니다. 그것들은 더 구체적이고 무한 루프로 쉽게 향하는 경향이 없습니다. – user3695782

+0

글쎄, 나는 'F'와 동등하지는 않지만 우리의 미로에서의 위치가 끝나기 때문에 특정 무언가를 시도했다. 그러나 while 루프에 printf 문을 넣어야 할 수도있다. 네가 말했듯이. 도와 줘서 고마워, 정말 고마워. – user3712556

관련 문제