2016-04-22 67 views
0

過去一週一直在處理此項目。 一切,但初始化/ mallocing和步功能是由我的教授事先提供給我...回溯迷宮算法在C中出現錯誤

目標不是創造迷宮,而是解決它。 我第一次得到它編譯它似乎運行良好,並會有所通過它...然後,它會開始做一些時髦的東西,並跳過牆壁,然後在那裏停止...

試圖修復它和經歷它。結束了它沒有第一步,現在它應該。試圖瞭解我做錯了,或者爲什麼我不能做正確的第一步,現在 在我maze_client ... //提供,但如果需要的話

#include <stdio.h> 
#include "maze.h" 

int main(int argc, char **argv) 
{ 
    FILE *fp = fopen(argv[1], "r"); 
    Maze maze = initializeMaze(fp); 
    findPath(maze);// first step.. It was provided to me. It basically calls step() 
    displayMaze(maze); 
    return 0; 
} 

步驟功能,我可以改一下...我做另一個不適合循環,我試圖看看我是否也可以擺脫廢話。沒有工作,只是讓它開始第一步,在其他地方隨機...第二個文件完成嘗試我阻止了每個功能......它似乎在我的if(getBottom)聲明......我可以寫下我的邏輯步驟是什麼我想,如果需要它做的事情......

static int step(Maze maze, int row, int column) 
{ 
    //TODO: FINISH THE BACKTRACKING ALGORITHM 
    int i,j; 
    //int st; 
    int successful; 
//maze->maze[row][column] = st; 

if(getVisit(maze, row, column) == maze->maze[maze->finishX][maze->finishY]) 
{ 
    displayMaze(maze); 
    return 1; 
} 

displayMaze(maze); 
getchar(); 
// getVisit(maze, row, column);//gets pos 
for(i = -2; i <= 2; ++i) 
{ 
    for(j = -2; j <= 2; ++j) 
    { 
    //i*i != j*j dont move in diagonal 
    //row+i >= 0 && row + i < n, bounds checking 
    //col+j >= 0 && col+j < n, bounds checking 
    if(i*i != j*j && row+i >= 0 && column+j >= 0) 
    { 
     getVisit(maze, row, column);//Gets position 
     if(getCellHasTop(maze, row, column + 1) != 1)//Wall check, Zero meaning NO WALL 
     { 
       //++st; 
       //gets that position and sees if it is unvisited... 
       //need a test if it is visited.. if it has been visited before.. Set current spot as BAD_PATH 
       if(getVisit(maze,row,column+j) == UNVISITED) 
       { 
        setVisit(maze, row, column+1, GOOD_PATH); 
        successful = step(maze, row, column + 1); 
       } 
       // if(getVisit(maze, row, column+j) == GOOD_PATH) 
       // { 
       // setVisit(maze, row-i, column, BAD_PATH); 
      // successful = step(maze, row, column+j); 
      //  } 
       if(successful) 
        return 1; 
     } 
     if(getCellHasLeft(maze,row - 1,column) != 1) 
     { 
       if(getVisit(maze, row-1, column) == UNVISITED) 
       { 
       setVisit(maze, row-1, column, GOOD_PATH); 
       successful = step(maze, row-1, column); 
       } 

       // if(getVisit(maze, row-i, column) == GOOD_PATH) 
       // { 
       //setVisit(maze, row-i, column, BAD_PATH); 
       // successful = step(maze, row-i, column); 
       // } 
       //++st; 
       if(successful) 
       return 1; 
     } 
     if(getCellHasRight(maze,row + 1, column) != 1) 
     { 
       if(getVisit(maze, row+1, column) == UNVISITED) 
       { 
        setVisit(maze, row+1, column, GOOD_PATH); 
        successful = step(maze, row+1, column); 
       } 

       // if(getVisit(maze, row+i, column) == GOOD_PATH) 
    //    { 
    //   setVisit(maze, row+i, column, BAD_PATH); 
     //   successful = step(maze, row+i, column); 
     //  } 
       //++st; 
       if(successful) 
       return 1; 
     } 
     if(getCellHasBottom(maze, row, column - 1) != 1) 
     { 
       if(getVisit(maze, row, column-1) == UNVISITED) 
       { 
        setVisit(maze, row, column-1, GOOD_PATH); 
        successful = step(maze, row, column-1); 
       } 

     //   if(getVisit(maze, row, column-j) == GOOD_PATH) 
     //  { 
      //  setVisit(maze, row, column-j, BAD_PATH); 
      // successful = step(maze, row, column-j); 
       // } 
       //++st; 
       if(successful) 
       return 1; 
     } 
     //PART 2 
     //Do not do else if because then each one would require... That is last resort 
    if(getCellHasTop(maze, row, column + 1) == 0)//Wall check 
     { 
       //++st; 
       //a test if it is visited.. if it has been visited before.. Set current spot as BAD_PATH 
       if(getVisit(maze,row, column+1) == GOOD_PATH) 
       { 
        setVisit(maze, row, column, BAD_PATH); 
        successful = step(maze, row, column + 1); 
       } 
       if(successful) 
       return 1; 
     } 
     if(getCellHasLeft(maze,row - 1,column) == 0) 
     { 
       if(getVisit(maze, row-1, column) == GOOD_PATH) 
       { 
       setVisit(maze, row, column, BAD_PATH); 
       successful = step(maze, row-1, column); 
       } 
    //++st; 
       if(successful) 
      return 1; 
} 
if(getCellHasRight(maze,row + 1, column) == 0) 
     { 
       if(getVisit(maze, row+1, column) == GOOD_PATH) 
       { 
       setVisit(maze, row, column, BAD_PATH); 
       successful = step(maze, row+1, column); 
       } 
       //++st; 
     if(successful) 
    return 1; 
     } 
     if(getCellHasBottom(maze, row, column - 1) == 0) 
    { 
       if(getVisit(maze, row, column-1) == GOOD_PATH) 
{ 
        setVisit(maze, row, column, BAD_PATH); 
        successful = step(maze, row, column-1); 
       } 
       //++st; 
       if(successful) 
       return 1; 
     } 
     //Part 3 
     if(getCellHasRight(maze, row + 1, column) != 0 && getCellHasLeft(maze,row - 1, column) != 0)// If there is a wall at those locationsZZ 
     { 
       if(getVisit(maze,row, column+1) == BAD_PATH) 
       { 
       setVisit(maze, row, column, BAD_PATH); 
     successful = step(maze, row, column-1); 
       } 
       if(successful) 
       return 1; 
} 
     if(getCellHasTop(maze, row, column + 1) != 0 && getCellHasBottom(maze, row, column - 1) != 0) 
    { 
       if(getVisit(maze, row-1, column) == BAD_PATH) 
       { 
       setVisit(maze, row, column, BAD_PATH); 
        successful = step(maze, row+1, column); 
       } 
       if(successful) 
       return 1; 
    } 
     if(getCellHasTop(maze, row, column +1) != 0 && getCellHasBottom(maze, row, column - 1) != 0) 
     { 
       if(getVisit(maze, row+1, column) == BAD_PATH) 
      { 
     setVisit(maze, row, column, BAD_PATH); 
      successful = step(maze, row-1, column); 
       } 
     if(successful) 
     return 1; 
     } 
     } 
    } 
    } 
     displayMaze(maze); 
     getchar(); 
    return 0; 
    } 
+1

看着你的代碼,你有幾個問題。最明顯的是從-2到+2行和列循環。爲什麼你會做任何循環,如果你有謂詞表示一個單元格是否有頂,左,右等?如果您可以從單元中獲取這些信息,則根本無需進行任何偏移計算 - 它們已經爲您完成。 –

+1

另外,我討厭你的函數名稱。真的,'getCellHasTop'?我得到的印象是你學校裏有人有一個嚴肅的Java戀物癖。爲什麼不'has_wall_above()'或'has_top()'?如果你堅持要求甚至'hasTop()'?我並不是說這只是爲了濫用 - 函數名稱應該是不言自明的,而你的名稱不是。我不知道'getVisit'是什麼意思,或者'getCellHasTop'。我必須閱讀代碼才能找出應該立即顯而易見的內容。 –

+1

下一個:當你有一個函數是一個*謂詞* - 也就是說,它存在回答一個問題 - 你不應該把它與任何東西進行比較。 **它是一個謂詞!**謂詞本質上是布爾型的,所以你只需在條件語句中使用它們的返回值:或者if(getCellHasTop(maze,row,col)){'或'if(!getCellHasTop(maze,行,列)){' –

回答

0

我認爲你的問題在於這樣的代碼:

if(getCellHasTop(maze, row, column + 1) != 1)//Wall check, Zero meaning NO WALL 
    { 
      //++st; 
      //gets that position and sees if it is unvisited... 
      //need a test if it is visited.. if it has been visited before.. Set current spot as BAD_PATH 
      if(getVisit(maze,row,column+j) == UNVISITED) 
      { 
       setVisit(maze, row, column+1, GOOD_PATH); 
       successful = step(maze, row, column + 1); 
      } 
      // if(getVisit(maze, row, column+j) == GOOD_PATH) 
      // { 
      // setVisit(maze, row-i, column, BAD_PATH); 
     // successful = step(maze, row, column+j); 
     //  } 
      if(successful) 
       return 1; 
    } 

首先,你迭代從-2到+2。但我認爲迷宮細胞是連續的,所以你不應該那樣做。相反,做這樣的事情: 首先,測試行,列確定它們在迷宮中的有效位置。接下來,測試當前單元格的位數,看看您是否被允許上升或任何方向。然後測試以查看目標單元格是否有效。然後測試目標單元格是否已被您的路徑阻擋。然後跳入。

enum { 
    TOP_BLOCKED = 0x01, 
    RIGHT_BLOCKED = 0x02, 
    BOTTOM_BLOCKED = 0x04, 
    LEFT_BLOCKED = 0x08, 
}; 

#define ABOVE(r,c)  (r),((c)+1) 
#define BELOW(r,c)  (r),((c)-1) 
#define LEFT_OF(r,c) ((r)-1),(c) 
#define RIGHT_OF(r,c) ((r)+1),(c) 

#define IN_MAZE(m,r,c) ((r)>=0 && (c)>=0 && (r)<(m)->rows && (c)<(m)->cols) 
#define CELL(m,r,c) ((m)->maze[(r)][(c)]) 
#define TOP_OPEN(m,r,c) (in_maze(m, r, c) \ 
         && !(CELL(m,r,c) & TOP_BLOCKED) 
         && in_maze(m,ABOVE(r,c))) 
#define VISITED(m,r,c) (getVisit(m,r,c) != UNVISITED) 


// ... later, in your step function ... 

#define POS row,column 

setVisit(maze, POS, GOOD_PATH); 

if (TOP_OPEN(maze, POS) && !VISITED(maze, ABOVE(POS)) && step(maze, ABOVE(POS))) { 
    // We found a successful path! Stop looking. 
    return 1; 
} 

if (RIGHT_OPEN(maze, POS) ... && step(maze, RIGHT_OF(POS))) { 
    return 1; 
} 

if (BOTTOM_OPEN ... 

if (LEFT_OPEN ... 
    return 1; 
} 

// There is no path from here that solves the maze. 
setVisit(maze, POS, UNVISITED); 
#undef POS 

return 0;