過去一週一直在處理此項目。 一切,但初始化/ 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;
}
- 的功能全碼:http://pastebin.com/PtAncpqS
- 頭文件(提供):http://pastebin.com/zYdCFMfX
- 掃描儀(提供):http://pastebin.com/4nQdnXTP
- 掃描頭:http://pastebin.com/KjpFf83R
- 第二個嘗試:http://pastebin.com/8qyuqtEv
看着你的代碼,你有幾個問題。最明顯的是從-2到+2行和列循環。爲什麼你會做任何循環,如果你有謂詞表示一個單元格是否有頂,左,右等?如果您可以從單元中獲取這些信息,則根本無需進行任何偏移計算 - 它們已經爲您完成。 –
另外,我討厭你的函數名稱。真的,'getCellHasTop'?我得到的印象是你學校裏有人有一個嚴肅的Java戀物癖。爲什麼不'has_wall_above()'或'has_top()'?如果你堅持要求甚至'hasTop()'?我並不是說這只是爲了濫用 - 函數名稱應該是不言自明的,而你的名稱不是。我不知道'getVisit'是什麼意思,或者'getCellHasTop'。我必須閱讀代碼才能找出應該立即顯而易見的內容。 –
下一個:當你有一個函數是一個*謂詞* - 也就是說,它存在回答一個問題 - 你不應該把它與任何東西進行比較。 **它是一個謂詞!**謂詞本質上是布爾型的,所以你只需在條件語句中使用它們的返回值:或者if(getCellHasTop(maze,row,col)){'或'if(!getCellHasTop(maze,行,列)){' –