数据结构实验2-迷宫
2013-11-10 17:43
309 查看
#include<stdio.h> #include<stdlib.h> typedef enum { ERROR, OK } Status; typedef struct { int row, line; }PosType; typedef struct { int di, ord; PosType seat; }SElemType; typedef struct { SElemType * base; SElemType * top; int stacksize; }SqStack; Status InitStack(SqStack &S); Status Push(SqStack &S, SElemType &a); Status Pop(SqStack &S, SElemType &a); Status StackEmpty(SqStack S); Status MazePath(int maze[12][12], SqStack &S, PosType start, PosType end); void Initmaze(int maze[12][12], int size); void printmaze(int maze[12][12], int size); Status Pass(int maze[12][12], PosType CurPos); void Markfoot(int maze[12][12], PosType CurPos); PosType NextPos(PosType CurPos, int Dir); void printpath(int maze[12][12], SqStack S, int size); void main(void) { SqStack S; int size, maze[12][12]; for (int n = 0; n < 10; n++) { printf("创建一个正方形迷宫,请输入迷宫尺寸(注意不要大于10):\n"); scanf_s("%d", &size); if (size < 1 || size>10) { printf("输入错误!"); return; } Initmaze(maze, size); printmaze(maze, size); PosType start, end; printf("输入入口行坐标和列坐标:"); scanf_s("%d", &start.row); scanf_s("%d", &start.line); printf("输入出口行坐标和列坐标:"); scanf_s("%d", &end.row); scanf_s("%d", &end.line); if (MazePath(maze, S, start, end)) printpath(maze, S, size); else printf("找不到通路!\n\n"); } } Status MazePath(int maze[12][12], SqStack &S, PosType start, PosType end) { PosType curpos; int curstep; SElemType e; InitStack(S); curpos = start; curstep = 1; do { if (Pass(maze, curpos)) { Markfoot(maze, curpos);//如果0可以通过,则标记为1 e.di = 1;//从第一个方向开始 e.ord = curstep; e.seat = curpos; Push(S, e);//放入栈S if (curpos.row == end.row && curpos.line == end.line)//到达目的地,成功 return OK; curpos = NextPos(curpos, 1);//继续向下一个探索 curstep++; } else//不能通过的情况 { if (!StackEmpty(S))//栈不为空 { Pop(S, e);//弹出栈元素,改变方向 while (e.di == 4 && !StackEmpty(S)) { Markfoot(maze, e.seat);//标记为1 Pop(S, e);//弹出e } if (e.di < 4) { e.di++; Push(S, e); curpos = NextPos(e.seat, e.di); } } } } while (!StackEmpty(S)); return ERROR; } void Initmaze(int maze[12][12], int size) { char select; printf("选择创建方式 A:自动生成 B:手动创建\n"); label:scanf_s("%c", &select); if (select == 'a' || select == 'A') { for (int i = 0; i < size + 2; i++) maze[0][i] = 1; for (int i = 1; i < size + 1; i++) { maze[i][0] = 1; for (int j = 1; j < size + 1; j++) maze[i][j] = rand() % 2; maze[i][size + 1] = 1; } for (int i = 0; i < size + 2; i++) maze[size + 1][i] = 1; } else if (select == 'b' || select == 'B') { printf("按行输入%d*%d数据,0代表可通,1代表不可通(每行以Enter结束):\n", size, size); for (int i = 0; i < size + 2; i++)maze[0][i] = 1; for (int i = 1; i < size + 1; i++) { maze[i][0] = 1; for (int j = 1; j < size + 1; j++) scanf_s("%d", &maze[i][j]); maze[i][size + 1] = 1; } for (int i = 0; i < size + 2; i++) maze[size + 1][i] = 1; } else if (select == '\n') goto label; else printf("输入错误!"); } void printmaze(int maze[12][12], int size)// { printf("\n\n"); printf("显示所建的迷宫(#表示外面的墙):\n"); for (int i = 0; i < size + 2; i++) printf("%c ", '#'); printf("\n"); for (int i = 1; i < size + 1; i++) { printf("%c ", '#'); for (int j = 1; j < size + 1; j++) { printf("%d ", maze[i][j]); } printf("%c", '#'); printf("\n"); } for (int i = 0; i < size + 2; i++) printf("%c ", '#'); printf("\n"); } void printpath(int maze[12][12], SqStack S, int size) { printf("\n\n通路路径为:\n"); SElemType * p = S.base; while (p != S.top) { maze[p->seat.row][p->seat.line] = 2; p++; } for (int i = 0; i < size + 2; i++) printf("%c ", '#'); printf("\n"); for (int i = 1; i < size + 1; i++) { printf("%c ", '#'); for (int j = 1; j < size + 1; j++) { if (maze[i][j] == 2) printf("%c ", '0'); else printf(" "); } printf("%c", '#'); printf("\n"); } for (int i = 0; i < size + 2; i++) printf("%c ", '#'); printf("\n\n"); } Status Pass(int maze[12][12], PosType CurPos) { if (maze[CurPos.row][CurPos.line] == 0) return OK; else return ERROR; } void Markfoot(int maze[12][12], PosType CurPos) { maze[CurPos.row][CurPos.line] = 1; } PosType NextPos(PosType CurPos, int Dir) { PosType ReturnPos; switch (Dir) { case 1: ReturnPos.row = CurPos.row; ReturnPos.line = CurPos.line + 1; break; case 2: ReturnPos.row = CurPos.row + 1; ReturnPos.line = CurPos.line; break; case 3: ReturnPos.row = CurPos.row; ReturnPos.line = CurPos.line - 1; break; case 4: ReturnPos.row = CurPos.row - 1; ReturnPos.line = CurPos.line; break; } return ReturnPos; } Status InitStack(SqStack &S) { S.base = (SElemType *) malloc(100 * sizeof(SElemType)); if (!S.base)return ERROR; S.top = S.base; S.stacksize = 100; return OK; } Status Push(SqStack &S, SElemType &a) { *S.top++ = a; return OK; } Status Pop(SqStack &S, SElemType &a) { if (S.top == S.base) return ERROR; a = *--S.top; return OK; } Status StackEmpty(SqStack S) { if (S.top == S.base) return OK; return ERROR; }
相关文章推荐
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索(超详细解释)
- SDUT 3361 数据结构实验之图论四:迷宫探索(DFS)
- 数据结构实验之图论四:迷宫探索
- 3361 数据结构实验之图论四:迷宫探索
- 3364-数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索
- SDUT 数据结构实验之图论四:迷宫探索 (DFS)
- 【数据结构实验】编制一个程序求解迷宫通路
- SDUTOJ3361-数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索(邻接表与DFS)
- 数据结构实验之图论四:迷宫探索
- 数据结构与算法-实验3-自定义栈,并实现走迷宫问题
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索
- sdutacm-数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索