ngaut

asm/c/c++/......

常用链接

统计

积分与排名

others

something special

经典的c/c++

朋友的网上家园

最新评论

数据结构笔记:递归的魅力——迷宫求解

代码参考<<java软件结构:设计和使用数据结构第二版>>,该书采用java实现,我自己用c写了一遍。

另外提醒一下:个人觉得该书翻译一般,排版较差,代码错误超多^_^,看得郁闷,不推荐。

不过总算是显示了递归的魅力,偶觉得递归实在太漂亮了^_^



maze.bmp

/********************************************************************
    created:    2005/12/23
    created:    23:12:2005   10:47
    filename:     maze.h
    author:        Liu Qi
    
    purpose:    递归走迷宫,如果有bug,请联系 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:        创建一个迷宫,该迷宫用二维数组模拟
* Return value:        指向迷宫的指针
* Author:            Liu Qi, //  [12/23/2005]
===========================================================================
*/

int** CreateMaze( void )
{
    
int i = 0;
    
int j = 0;

    
//    设置迷宫为:
    
//        1 1 1
    
//        1 0 1
    
//        0 1 1
    
//  1:(通道)运行通过,0:(墙)不许通过
    int Maze[COLUMN][ROW] =
    
{
        
{111},
        
{101},
        
{011}
    }
;

    
//给二维数组分配空间,有点麻烦哦,我也是今天才明白,
    
//对比之下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: 指向迷宫(二维数组)的指针
* Precondition:        NULL != pMaze
* Description:        销毁迷宫
* 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:二维数组
* Precondition:        void
* Description:        显示迷宫(二维数组)
* 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:迷宫(二维数组), pos:位置
* Precondition:        void
* Description:        检测指定位置是否合法
* 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))
    
{
        
//当前位置不会是墙吧^_^, 1表示允许通过
        if ( pMaze[col][row] == 1 )
        
{
            bRet 
= true;
        }

    }


    
return bRet;
}





/*===========================================================================
* Function name:    Traverse
* Parameter:        pMaze, col, row
* Precondition:        NULL != pMaze
* Description:        递归的遍历迷宫的每个单元
* Return value:        如果该单元能透过则返回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

posted on 2005-12-23 21:04 ngaut 阅读(2310) 评论(5)  编辑 收藏 引用 所属分类: c/c++/ds

评论

# re: 数据结构笔记:递归的魅力——迷宫求解 2006-06-19 20:21 鲤鱼

时间复杂度怎么算的啊?  回复  更多评论   

# re: 数据结构笔记:递归的魅力——迷宫求解 2006-12-13 15:36

你的程序是不错!
但是我怎么就运行不出来啊~
数据结构怎么都不行~
郁闷~~~~  回复  更多评论   

# re: 数据结构笔记:递归的魅力——迷宫求解[未登录] 2007-07-21 12:14 YY

//递归出口
if ((col == COLUMN - 1 ) && ( col == ROW - 1))
{
bRet = true;
}
应该是写错了,改为
//递归出口
if ((col == COLUMN - 1 ) && ( row == ROW - 1))
{
bRet = true;
}
  回复  更多评论   

# re: 数据结构笔记:递归的魅力——迷宫求解 2008-03-14 21:07

... 真难看懂..我的程序设计课怎么过啊...哭...  回复  更多评论   

# re: 数据结构笔记:递归的魅力——迷宫求解 2008-06-03 19:23 呵呵

//迷宫求解 递归

#include <stdio.h>
#define M 10
#define N 10

typedef struct { //组成迷宫的基本元素
long data; //data为0时 不可通路 为一时 可通
long next; //指向下一步 应该探索的方向 1 2 3 4分别表示上下左右;当为5时 代表四

个方向 探索失败
long x, y; //暂时无用 设想储存当前位置
}elem;

long enter_x=2, enter_y=2; //迷宫入口
long exit_x=9, exit_y=9; //迷宫出口
long flag=0;

long Test( elem **S, long point) //探测point方向上的相邻一格 是否可通
{
if(point == 1)
if( S[0][-1].data == 0 || S[0][-1].data == 2 ) return 0;
else return 1;
else if(point == 2)
if( S[1][0].data == 0 || S[1][0].data == 2 ) return 0;
else return 1;
else if(point == 3)
if( S[0][1].data == 0 || S[0][1].data == 2 ) return 0;
else return 1;
else if(point == 4)
if( S[-1][0].data == 0 || S[-1][0].data == 2 ) return 0;
else return 1;
}

elem *GetNext( elem **S, long point) //得到可通的相邻格的地址
{
if(point == 1)
return &(S[0][-1]);
if(point == 2)
return &(S[1][0]);
if(point == 3)
return &(S[0][1]);
if(point == 4)
return &(S[-1][0]);
}

haha( elem *T ) //递归主体
{
if(flag) return 0; //当标志位 被置为1时 代表已找到通路 退出
if(T->x==exit_x && T->y==exit_y ) {
flag = 1;
return 0;
}
for(;;) {
if(T->next==5)
return 0;
if( Test(&T, T->next) ) {
(T->next)++;
T->data=2;
haha( GetNext(&T, T->next) );
}
else (T->next)++;
}
return 0;
}

main()
{
elem str[M][N];
long i, j;
for(i=1; i<=M; i++) //建立迷宫
for(j=1; j<=N; j++) {
printf("iput the str[%d][%d]:", i, j);
scanf("%d", &(str[i][j].data) );
str[i][j].next=1;
str[i][j].x=i;
str[i][j].y=j;
}
printf("Created\n");
haha( &(str[enter_x][enter_y]) );
for(i=1; i<=M; i++) {
putchar(10);
for(j=1; j<=N; j++)
printf("%d ", str[i][j].data);
}
return 0;
}

高手 指点一下吧 这个是自己写的 写了一个小时 但是 调了一个小时了 还是过不了 用vc写的 在gcc下 干脆建迷宫时 就出错了 看看哪里不对吧

  回复  更多评论   


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]

相关链接:
网站导航: