|
|
|
發(fā)新文章 |
|
|
代碼參考<<java軟件結(jié)構(gòu):設(shè)計(jì)和使用數(shù)據(jù)結(jié)構(gòu)第二版>>,該書(shū)采用java實(shí)現(xiàn),我自己用c寫(xiě)了一遍。
另外提醒一下:個(gè)人覺(jué)得該書(shū)翻譯一般,排版較差,代碼錯(cuò)誤超多^_^,看得郁悶,不推薦。
不過(guò)總算是顯示了遞歸的魅力,偶覺(jué)得遞歸實(shí)在太漂亮了^_^

 /**//********************************************************************
created: 2005/12/23
created: 23:12:2005 10:47
filename: maze.h
author: Liu Qi
purpose: 遞歸走迷宮,如果有bug,請(qǐng)聯(lián)系 ngaut#126.com
*********************************************************************/


#ifndef MAZE_H
#define MAZE_H


#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#include <assert.h>


#define COLUMN 3
#define ROW 3

#define TRIED 5
#define PASSED 6



 /**//*===========================================================================
* Function name: CreateMaze
* Parameter: void
* Precondition: void
* Description: 創(chuàng)建一個(gè)迷宮,該迷宮用二維數(shù)組模擬
* Return value: 指向迷宮的指針
* Author: Liu Qi, // [12/23/2005]
===========================================================================*/
int** CreateMaze( void )
  {
int i = 0;
int j = 0;

// 設(shè)置迷宮為:
// 1 1 1
// 1 0 1
// 0 1 1
// 1:(通道)運(yùn)行通過(guò),0:(墻)不許通過(guò)
int Maze[COLUMN][ROW] =
 {
 {1, 1, 1},
 {1, 0, 1},
 {0, 1, 1}
};

//給二維數(shù)組分配空間,有點(diǎn)麻煩哦,我也是今天才明白,
//對(duì)比之下STL的vector真好使^_^
int** pMaze = (int **) malloc (COLUMN * sizeof(int*));
assert( NULL != pMaze );
for ( i = 0; i < COLUMN; i++ )
 {
pMaze[i] = (int *) malloc ( ROW * sizeof(int) );
}


for ( i = 0; i < COLUMN; i++ )
 {
for ( j = 0; j < ROW; j++)
 {
pMaze[i][j] = Maze[i][j];
}
}

return pMaze;
}


 /**//*===========================================================================
* Function name: DestoryMaze
* Parameter: pMaze: 指向迷宮(二維數(shù)組)的指針
* Precondition: NULL != pMaze
* Description: 銷(xiāo)毀迷宮
* Return value: void
* Author: Liu Qi, // [12/23/2005]
===========================================================================*/
void DestoryMaze( int** pMaze )
  {
int i;
assert( NULL != pMaze );

for ( i = 0; i < COLUMN; i++)
 {
free( pMaze[i] );
}

free(pMaze);
}



 /**//*===========================================================================
* Function name: DisplayMaze
* Parameter: Maze:二維數(shù)組
* Precondition: void
* Description: 顯示迷宮(二維數(shù)組)
* Return value: void
* Author: Liu Qi, [12/23/2005]
===========================================================================*/
void DisplayMaze(int** pMaze)
  {
int i = 0;
int j = 0;

assert( NULL != pMaze );

for ( ; i < COLUMN; i++ )
 {
for (j = 0; j < ROW; j++)
 {
printf(" %d ", pMaze[i][j]);
}

printf("\n");
}
}



 /**//*===========================================================================
* Function name: ValidPosition
* Parameter: Maze:迷宮(二維數(shù)組), pos:位置
* Precondition: void
* Description: 檢測(cè)指定位置是否合法
* Return value: true:合法, false:不合法
* Author: Liu Qi, [12/23/2005]
===========================================================================*/
bool ValidPosition(int** pMaze, int col, int row)
  {
bool bRet = false;

assert( NULL != pMaze );

//位置是否越界
if ((col >= 0 && col < COLUMN)
&& (row >= 0 && row < ROW))
 {
//當(dāng)前位置不會(huì)是墻吧^_^, 1表示允許通過(guò)
if ( pMaze[col][row] == 1 )
 {
bRet = true;
}
}

return bRet;
}




 /**//*===========================================================================
* Function name: Traverse
* Parameter: pMaze, col, row
* Precondition: NULL != pMaze
* Description: 遞歸的遍歷迷宮的每個(gè)單元
* Return value: 如果該單元能透過(guò)則返回true,否則返回false
* Author: Liu Qi, [12/23/2005]
===========================================================================*/
bool Traverse(int **pMaze, int col, int row)
  {
bool bRet = false;

assert( NULL != pMaze );

if (ValidPosition(pMaze, col, row))
 {
pMaze[col][row] = TRIED;

printf("tring [%d][%d]\n", col, row);

//遞歸出口
if ((col == COLUMN - 1 ) && ( col == ROW - 1))
 {
bRet = true;
}
else
 {
bRet = Traverse(pMaze, col + 1, row); //down
if ( !bRet )
 {
bRet = Traverse(pMaze, col, row + 1); //right
}
if ( !bRet )
 {
bRet = Traverse(pMaze, col - 1, row); //up
}
if ( !bRet )
 {
bRet = Traverse(pMaze, col, row - 1); //left
}
}

if ( !bRet )
 {
pMaze[col][row] = PASSED;
}
}

return bRet;
}


#endif

|