数据结构迷宫源程序
来源:互联网 发布:海康监控数据恢复 编辑:程序博客网 时间:2024/05/29 01:55
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define M 100
#define TRUE 1
#define FALSE 0
#define OVERFLOW 0
#define OK 1
#define ERROR 0
int a[10][10] = {{1, 1, 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, 1, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},};
typedef struct
{
int r;
int c;
}postype;
typedef struct
{
int ord;//通道口在路径上的序号
postype seat;//通道口在迷宫中的位置
int di;//从通道口走向下一通道口的方向
}selmtype;
typedef struct
{
selmtype *base;
selmtype *top;
int stacksize;
}sqstack;
int initstack(sqstack &s);
int push(sqstack &s,selmtype &e);
int pop(sqstack &s,selmtype &e);
int stackempty(sqstack s);
selmtype gettop(sqstack s);
int pass(postype curpos);
void footprint(postype curpos);
postype nextpos(postype curpos,int i);
void markprint(postype m);
int main()
{
int i, j;
int curstep=1,b=0;
postype start={1,1},end={8,8},curpos;
sqstack s;
selmtype e;
initstack(s);
curpos=start;
printf("还没有走的迷宫\n");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
printf("%d\t", a[i][j]);
printf("\n\n");
}
do{
if(b=pass(curpos))
{
footprint(curpos);
e.ord=curstep;
e.seat=curpos;
e.di=1;
push(s,e);
if(curpos.r==end.r && curpos.c==end.c)
{
printf("迷宫成功走完了!!!");
break;
}
curpos = nextpos(curpos,1);
curstep++;
}
else
{
if(!stackempty(s))
{ //判断栈是否为空。
pop(s, e);
while(e.di==4 && !stackempty(s))
{
markprint(e.seat);//留下不能通过的标记。
pop(s,e); //退回一步.
}
if(e.di<4)
{
e.di++;push(s,e);
curpos = nextpos(e.seat,e.di);
}
}
}
}while(!stackempty(s));
printf("\n已经走完的迷宫\n");
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
printf("%d\t",a[i][j]);
printf("\n\n");
}
return 0;
}
int initstack(sqstack &s)
{
s.base=(selmtype*)malloc(M*sizeof(selmtype));
if(!s.base) return ERROR;
s.top=s.base;
s.stacksize=M;
return OK;
}
int push(sqstack &s,selmtype &e)
{
*s.top++=e;
return OK;
}
int pop(sqstack &s,selmtype &e)
{
if(s.top==s.base) return ERROR;
e=*--s.top;
return OK;
}
int stackempty(sqstack s)
{
if(s.top==s.base) return TRUE;
else return FALSE;
}
selmtype gettop(sqstack s)
{
selmtype e;
if(s.top != s.base)
e = *(s.top - 1);
return e;
}
int pass(postype curpos)
{
int i=curpos.r;
int j=curpos.c;
if(a[i][j]==0) return 1;
else return 0;
}
void footprint(postype curpos)
{
int i=curpos.r;
int j=curpos.c;
a[i][j]='9';
}
postype nextpos(postype curpos,int i)
{
switch(i)
{
case 1:curpos.c+=1;break;
case 2:curpos.r+=1;break;
case 3:curpos.c-=1;break;
case 4:curpos.r-=1;break;
default:exit(ERROR);
}
return curpos;
}
void markprint(postype m)
{
int i,j;
i=m.r;
j=m.c;
a[i][j]='4';
}
#include<stdlib.h>
#include<malloc.h>
#define M 100
#define TRUE 1
#define FALSE 0
#define OVERFLOW 0
#define OK 1
#define ERROR 0
int a[10][10] = {{1, 1, 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, 1, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},};
typedef struct
{
int r;
int c;
}postype;
typedef struct
{
int ord;//通道口在路径上的序号
postype seat;//通道口在迷宫中的位置
int di;//从通道口走向下一通道口的方向
}selmtype;
typedef struct
{
selmtype *base;
selmtype *top;
int stacksize;
}sqstack;
int initstack(sqstack &s);
int push(sqstack &s,selmtype &e);
int pop(sqstack &s,selmtype &e);
int stackempty(sqstack s);
selmtype gettop(sqstack s);
int pass(postype curpos);
void footprint(postype curpos);
postype nextpos(postype curpos,int i);
void markprint(postype m);
int main()
{
int i, j;
int curstep=1,b=0;
postype start={1,1},end={8,8},curpos;
sqstack s;
selmtype e;
initstack(s);
curpos=start;
printf("还没有走的迷宫\n");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
printf("%d\t", a[i][j]);
printf("\n\n");
}
do{
if(b=pass(curpos))
{
footprint(curpos);
e.ord=curstep;
e.seat=curpos;
e.di=1;
push(s,e);
if(curpos.r==end.r && curpos.c==end.c)
{
printf("迷宫成功走完了!!!");
break;
}
curpos = nextpos(curpos,1);
curstep++;
}
else
{
if(!stackempty(s))
{ //判断栈是否为空。
pop(s, e);
while(e.di==4 && !stackempty(s))
{
markprint(e.seat);//留下不能通过的标记。
pop(s,e); //退回一步.
}
if(e.di<4)
{
e.di++;push(s,e);
curpos = nextpos(e.seat,e.di);
}
}
}
}while(!stackempty(s));
printf("\n已经走完的迷宫\n");
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
printf("%d\t",a[i][j]);
printf("\n\n");
}
return 0;
}
int initstack(sqstack &s)
{
s.base=(selmtype*)malloc(M*sizeof(selmtype));
if(!s.base) return ERROR;
s.top=s.base;
s.stacksize=M;
return OK;
}
int push(sqstack &s,selmtype &e)
{
*s.top++=e;
return OK;
}
int pop(sqstack &s,selmtype &e)
{
if(s.top==s.base) return ERROR;
e=*--s.top;
return OK;
}
int stackempty(sqstack s)
{
if(s.top==s.base) return TRUE;
else return FALSE;
}
selmtype gettop(sqstack s)
{
selmtype e;
if(s.top != s.base)
e = *(s.top - 1);
return e;
}
int pass(postype curpos)
{
int i=curpos.r;
int j=curpos.c;
if(a[i][j]==0) return 1;
else return 0;
}
void footprint(postype curpos)
{
int i=curpos.r;
int j=curpos.c;
a[i][j]='9';
}
postype nextpos(postype curpos,int i)
{
switch(i)
{
case 1:curpos.c+=1;break;
case 2:curpos.r+=1;break;
case 3:curpos.c-=1;break;
case 4:curpos.r-=1;break;
default:exit(ERROR);
}
return curpos;
}
void markprint(postype m)
{
int i,j;
i=m.r;
j=m.c;
a[i][j]='4';
}
0 0
- 数据结构迷宫源程序
- 数据结构--迷宫
- 哈夫曼编码(数据结构)源程序
- 数据结构二叉树源程序
- 数据结构赫夫曼树源程序
- 数据结构实习:迷宫求解
- 迷宫MAZE(数据结构)
- 数据结构学习笔记--迷宫
- 迷宫求解(数据结构)
- 数据结构 迷宫 代码
- 数据结构-迷宫求解路径
- 数据结构之迷宫
- 数据结构之迷宫
- 数据结构迷宫破解
- 数据结构课程设计---迷宫
- 数据结构迷宫升级版
- 数据结构-迷宫实验
- 数据结构迷宫问题
- 查询所有项目中所有字体
- C#里面MessageBox不能乱用,阻塞和非阻塞,模态对话框和非模态
- Laravel框架如何使用阿里云ACE缓存服务
- CSU 1111 三家人
- |
- 数据结构迷宫源程序
- 数据挖掘十大算法---EM 期望最大算法
- java中如何导入03和07版的Excel文件数据,存入到数据库
- NIT-三个师妹之点菜
- ios开发中,关于javascript直接调用oc代码而非通过改变url回调方式
- 错误列表集锦
- 数据结构二叉树源程序
- 从程序员的角度谈创业三年的亲身体会
- sphinx windows安装25个常见问题+win7系统中要注意的事项