手机版
你好,游客 登录 注册
背景:
阅读新闻

C语言 迷宫问题(堆栈及其应用)

[日期:2014-07-20] 来源:Linux社区  作者:linux_player_c [字体:大 中 小]

二. 迷宫问题
           
            迷宫问题跟那个电老鼠有很多相似之处,但只是在软件级别上的问题,简单了不少。迷宫问题要解决一下几个方面:
            1.如何存储合理位置的信息(当然是用堆栈了);
            2.移动方向的判断(这个和醉酒螳螂有着极大的相似之处,还是把八个方向用一个结构体数组存储,看来这个方法经常用啊);
            3.不可以在迷宫上直接操作,需要用另外一个二维数组动态显示位置变化;
 
            还需解决的问题:
           
            如何动态的生成一个迷宫?以后再看;
            迷宫搜索算法有没有比单方向查找更快的方法?大家一起看;
           
            迷宫的代码就摆出来了。。。
 

 /*
迷宫问题:使用到了堆栈,原理是对的,但是在显示的时候太过狗血,而且路径没有正向输出,大家凑活着看吧。。。。
 
*/
 
#include<stdio.h>
 
#include<conio.h>
 
#include<windows.h>
 

 

typedef struct OFFSETS
 
{
 
short int vert;
 //横方向
 
short int horiz;
 //纵方向
 
}OFFSETS;
 

 

 


//要保存的每个节点信息
 
typedef struct element
 
{
 
short int row;
 
short int col;
 
short int direct;
 
}element;
 

 

typedef element USER_TYPE;
 

 

#include"STACK.H"  //堆栈
 

 

#define MAZE_ROWS  11
 //迷宫行数
 
#define MAZE_COLS  15
 //迷宫列数
 
#define BOUNDARY_COLS  17  //边界列数
 
#define BOUNDARY_ROWS  13  //边界行数
 
#define FALSE  0
 
#define TRUE  1
 

 

//生成一个迷宫的图形(这尼玛是要累死人啊!!!)
 
static short int maze[BOUNDARY_ROWS][BOUNDARY_COLS] =
 
{
 
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 
{1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1},
 
{1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1},
 
{1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1},
 
{1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1},
 
{1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
 
{1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1},
 
{1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 
{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1},
 
{1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1},
 
{1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1},
 
{1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1},
 
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 
};
 

 

//该矩阵是为了记录已经检查过的迷宫位置,元素初始化都为0
 
static short int mark[BOUNDARY_ROWS][BOUNDARY_COLS] = {0};
 

 

OFFSETS move[8];  /*当前点对应的八个方向*/
 
STACK *nodeStack;
 

 

void initMove(void);  //初始化各方向
 
void showMaze(void);  //显示迷宫
 
void foundPath();  //寻找路径
 
void printOneNode(int row, int col, int direct);
 //打印一个节点
 
void showMark(void);  //显示路径
 

 

void showMark(void)
 
{
 
int i, j;
 

 

for(i = 0; i < BOUNDARY_ROWS; i++)
 
{
 
for(j = 0; j < BOUNDARY_COLS; j++)
 
if(mark[i][j] == 1)
 
printf("%3c", 5);
 
else
 
printf("%3c", 3);
 
printf("\n");
 
}
 
}
 

 

void printOneNode(int row, int col, int direct)
 
{
 
printf("%2d    %2d%5d", direct, row, col);
 
switch(direct - 1)
 
{
 
case 0:
 
printf("    北边");
 
break;
 
case 1:
 
printf("    东北");
 
break;
 
case 2:
 
printf("    东边");
 
break;
 
case 3:
 
printf("    东南");
 
break;
 
case 4:
 
printf("    南边");
 
break;
 
case 5:
 
printf("    西南");
 
break;
 
case 6:
 
printf("    西边");
 
break;
 
case 7:
 
printf("    西北");
 
break;
 
}
 
printf("\n");
 

 

}
 

 

void foundPath()
 
{
 
int i, row, col, nextRow, nextCol, direct, count;
 
int found = FALSE;
 //初始化为没有找到出口
 
USER_TYPE
 position;
 

 

mark[1][1] = 1;
 //开始处于(1,1)迷宫入口
 
position.row = 1, position.col = 1, position.direct = 0;
 
push(nodeStack, position);

 

 

//节点栈不为空并且没有找到出口
 
while(isStackEmpty(*nodeStack) == FALSE && found == FALSE)
 
{
 
pop(nodeStack, &position);
 
row = position.row;
 
col = position.col;
 
direct = position.direct;
 

 

while(direct < 8 && found == FALSE)
 
{
 
nextRow = row + move[direct].vert;
 
nextCol = col + move[direct].horiz;
 
//如果找到了出口
 
if(nextRow == MAZE_ROWS + 1 && nextCol == MAZE_COLS + 1)
 
found = TRUE;
 
//当前点没有被检查过
 
else if(!maze[nextRow][nextCol]
 
 && !mark[nextRow][nextCol])
 
{
 
mark[nextRow][nextCol] = 1;
 
position.row = row;
 
position.col= col;
 
position.direct = ++direct;

 
push(nodeStack, position);
 //将这个节点保存
 

 

row = nextRow;
 
col = nextCol;
 
direct = 0;
 
system("cls");
 
showMaze();
 
printf("\n");
 
printf("上一个图为迷宫图,下一个图是寻找路径的过程\n");
 
showMark();
 
Sleep(200);
 //停顿一秒
 
}
 
else
 
direct++;
 //换个方向测试
 
}
 
}
 

 

if(found == FALSE)
 
printf("该迷宫没有路径可以出去!");
 
else
 
{
 
count = stackLength(*nodeStack);
 

 

printf("该迷宫的出口路径是:\n\n");
 
printf("dir#  rol  col  direct\n\n");
 

 

//打印出路径
 
for(i = 0; i < count; i++)
 
{
 
pop(nodeStack, &position);
 
printOneNode(position.row, position.col, position.direct);
 

 

 

}
 
}
 
}
 

 

void showMaze(void)
 
{
 
int i, j;
 

 

for(i = 0; i < BOUNDARY_ROWS; i++)
 
{
 
for(j = 0; j < BOUNDARY_COLS; j++)
 
if(maze[i][j] == 1)
 
printf("%3c", 3);
 
else
 
printf("%3c", 5);
 
printf("\n");
 
}
 
}
 

 

void initMove(void)
 
{
 
move[0].vert = -1, move[0].horiz = 0;
 /* 北边 */
 
move[1].vert = -1, move[1].horiz = 1;
 /* 东北 */
 
move[2].vert = 0, move[2].horiz = 1;
 /* 东边 */
 
move[3].vert = 1, move[3].horiz = 1;
 /* 东南 */
 
move[4].vert = 1, move[4].horiz = 0;
 /* 南边 */
 
move[5].vert = 1, move[5].horiz = -1;
 /* 西南 */
 
move[6].vert = 0, move[6].horiz = -1;
 /* 西边 */
 
move[7].vert = -1, move[7].horiz = -1;
 /* 西北 */
 


}
 

 

int main(int argc, char **argv)
 
{
 
nodeStack = initStack(20);
 //初始化堆栈(起始数量为20个,如果堆栈内元素判满时,自动追加10个元素)
 


initMove();
 //初始化各方向
 
showMaze();
 //显示迷宫
 
foundPath();
 //寻找路径
 
destroyStack(&nodeStack);
 

 

getch();
 
return 0;
 
}
 


关于堆栈的用法肯定会是多种多样的,咱们一起来探索。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-07/104455.htm

linux
相关资讯       C语言  迷宫问题  堆栈 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款