严蔚敏迷宫程序

来源:互联网 发布:新加坡 人工智能 编辑:程序博客网 时间:2024/05/16 06:05

看了一晚上才都弄懂。。。水平好次。。。   T^T

solve函数是关键。

// maze.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <malloc.h>#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR -1#define MAXSIZE 10#define TRUE 1#define FALSE 0typedef enum direction{right,down,left,up};typedef enum markflag{yes,no};typedef struct Position{int x;int y;}position;typedef struct{int order;position seat;direction di;}selemtype;typedef struct{int top;selemtype* elem;}stack;char maze[MAXSIZE][MAXSIZE] = {{ '0', '0', '1', '1', '1', '1', '1', '1', '1', '1' },{ '1', '0', '0', '1', '0', '0', '0', '1', '0', '1' },{ '1', '0', '0', '1', '0', '0', '0', '1', '0', '1' },{ '1', '0', '0', '0', '0', '1', '1', '0', '0', '1' },{ '1', '0', '1', '1', '1', '0', '0', '0', '0', '1' },{ '1', '0', '0', '0', '1', '0', '0', '0', '0', '1' },{ '1', '0', '1', '0', '0', '0', '1', '0', '0', '1' },{ '1', '0', '1', '1', '1', '0', '1', '1', '0', '1' },{ '1', '1', '0', '0', '0', '0', '0', '0', '0', '1' },{ '1', '1', '1', '1', '1', '1', '1', '1', '0', '1' }};//-------------------------------------------------------------------------int init_stack(stack* s);int push(stack* s, selemtype e);int pop(stack* s, selemtype* e);int empty(stack* s);int creata_maze(position* startpos, position* endpos);int can_pass(position pos);int mark_pos(position curpos, markflag flag);position next_pos(position curpos, direction dir);direction next_dir(direction dir);int solve(stack* s, position start, position end);//------------------------------------------------------------------------ -int init_stack(stack* s){s->elem = (selemtype*)malloc(MAXSIZE*MAXSIZE*sizeof(selemtype));s->top = 0;if (!s->elem)return ERROR;return OK;}int push(stack* s, selemtype e){if (s->top>MAXSIZE*MAXSIZE)return ERROR;s->top++;s->elem[s->top] = e;return OK;}int pop(stack* s, selemtype* e){if (s->top <= 0)return ERROR;*e = s->elem[s->top];s->top--;return OK;}int empty(stack* s){if (s->top == 0)return TRUE;return FALSE;}int creata_maze(position* startpos, position* endpos){position start, end;printf("请输入迷宫入口:");scanf("%d %d", &start.x, &start.y);printf("请输入迷宫出口:");scanf("%d %d", &end.x, &end.y);*startpos = start;*endpos = end;return OK;}int can_pass(position pos){if (maze[pos.x][pos.y] == '0')return TRUE;return FALSE;}int mark_pos(position curpos, markflag flag){switch (flag){case yes:maze[curpos.x][curpos.y] = '.';break;case no:maze[curpos.x][curpos.y] = '#';break;}return OK;}position next_pos(position curpos, direction dir){switch (dir){case right:curpos.y++;break;case down:curpos.x++;break;case left:curpos.y--;break;case up:curpos.x--;break;}return curpos;}direction next_dir(direction dir){switch (dir){case right:return down;case down:return left;case left:return up;}}int solve(stack* s, position start, position end){position curpos;selemtype e;int curstep = 1;if (init_stack(s) == ERROR)return FALSE;curpos = start;do{if (can_pass(curpos)){mark_pos(curpos,yes);e.di = right;e.seat = curpos;e.order = curstep;push(s, e);if (curpos.x == end.x&&curpos.y == end.y)return TRUE;curpos = next_pos(curpos, right);curstep++;}else{if (!empty(s))         //出栈   获得上一个位置pop(s, &e);if (e.di != up){//上一个位置换方向走e.di = next_dir(e.di);push(s, e);curpos = next_pos(e.seat, e.di);//不是next_pos(curpos, e.di)    }while (e.di == up&&!empty(s)){//上一个位置四个方向都不行//令上一位置等于当前位置   回溯到有方向可走的pos 或是startposcurpos = e.seat;mark_pos(curpos, no);if (pop(s, &e) == ERROR)return FALSE;}}} while (!empty(s));return FALSE;}int _tmain(int argc, _TCHAR* argv[]){position startpos, endpos;stack path;selemtype e;int i, j;if (creata_maze(&startpos, &endpos) == ERROR)exit(1);solve(&path, startpos, endpos);while (!empty(&path)){pop(&path, &e);printf("%d%d", e.seat.x, e.seat.y);printf(" ");}printf("\n");for (i = 0; i < 10; i++){for (j = 0; j < 10; j++)printf("%c", maze[i][j]); printf("\n");}system("pause");return 0;}



原创粉丝点击