C语言迷宫求解 求解函数执行还有问题 大家帮忙看下啊,拜托了,很急~
//C语言迷宫求解 求解函数执行还有问题 大家帮忙看下啊,拜托了,很急~
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define ERROR -2
#define width 6 //迷宫地图宽度
#define height 6 //迷宫地图高度
#define STACK_INIT_SIZE 50 //栈存储空间初始分配量
#define STACKINCREMENT 10 //栈存储空间分配增量
typedef struct //游戏中的位置坐标
{//二维数组下标
int x;
int y;
}Position;
typedef struct
{
Position start; //起始位置
Position end; //结束位置
int curstep;
}MapCfg;
MapCfg map;
typedef struct
{
int ord; //通道块在路径上的序号
Position curpos; //通道块在迷宫中的位置坐标
int di; //从此通道块走向下一通道块的"方向"
int footprint; //是否可以通过的标记, -1:已访问 -2:不可通过
}SElemType;
SElemType e;
typedef struct{
SElemType* base; //栈底指针
SElemType* top; //栈顶指针
int stacksize; //栈的大小
}SqStack;
SqStack S; //S 探索迷宫 存放路径
int count = 0; //用于计数,判断次数
int **Maze()
{
int i=0,j=0;
int **maze; //定义二维指针存取迷宫
maze = new int *[height+2]; //申请长度等于行数加2 的二级指针
for(i= 0;i<height+2;i++) //申请每个二维指针的空间
{
maze[i] = new int[width+2];
}
/*
srand((unsigned)time(NULL)); //使用系统时间 传入空指针NULL 作为初始化种子
for(i=1;i<height+2;i++) //生成迷宫 0:墙 1:路
for(j=1;j<width+2;j++)
maze[i][j]=rand()%2; //产生随机数 0 1
*/
maze[1][2] = 0;maze[1][3] = 1;maze[1][4] =0;maze[1][5] =1;maze[1][6] =0;
maze[2][1] = 1;maze[2][2] = 1;maze[2][3] =0;maze[2][4] =0;maze[2][5] =1;maze[2][6] =0;
maze[3][1] = 0;maze[3][2] = 1;maze[3][3] =1;maze[3][4] =0;maze[3][5] =0;maze[3][6] =1;
maze[4][1] = 1;maze[4][2] = 0;maze[4][3] =1;maze[4][4] =1;maze[4][5] =1;maze[4][6] =1;
maze[5][1] = 1;maze[5][2] = 0;maze[5][3] =1;maze[5][4] =0;maze[5][5] =1;maze[5][6] =0;
maze[6][1] = 0;maze[6][2] = 1;maze[6][3] =1;maze[6][4] =0;maze[6][5] =1;
for(i=0;i<height+2;i++)
maze[i][0]=maze[i][width+1]=0;
for(j=0;j<width+2;j++)
maze[0][j]=maze[width+1][j]=0;
maze[1][1] = maze[height][width] =1; //指定入口与出口
return maze; //返回存贮迷宫的二维指针maze
}
int InitStack(SqStack &S) //初始化栈 用于探索 存放路径
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE *sizeof(SqStack));
if(!S.base)
{
return FALSE; //存储分配失败
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return TRUE;
}
int StackEmpty(SqStack &S) //若栈 S 为空栈,则返回 TRUE,否则返回 FALSE
{
if(S.top == S.base)
return TRUE;
else
return FALSE;
}
int Push(SqStack &S,SElemType e)
{
if(S.top - S.base >= S.stacksize) //栈满,追加存储空间
{
S.base = (SElemType *)realloc(S.base,(S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if( !S.base )
exit(OVERFLOW); //存储分配失败
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return TRUE;
}
int Pop(SqStack &S, SElemType &e) //若栈不空,则删除 S 的栈顶元素,用 e 返回其值,并返回 TRUE;否则返回ERROR
{
if(S.top == S.base)
return ERROR;
e = * --S.top;
return TRUE;
}
int Pass(int **temp, Position curpos) //判断当前位置可通,返回TRUE,否则返回FALSE
{
count++; //计数,判断次数
printf("第 %d 次判断!\n",count);
printf("当前位置迷宫元素: %d \n",temp[curpos.x][curpos.y]);
if(temp[curpos.y][curpos.x] == 1) //判断新位置是否可达
return TRUE;
else
return FALSE;
}
int FootPrint(int **m, Position curpos) //在当前位置标记已访问
{
m[curpos.x ][curpos.y ] = -1;
return TRUE;
}
int MarkPrint(int **m, Position curpos) //在当前位置标记不可通过
{
m[curpos.x ][curpos.y ] = -2;
return TRUE;
}
int StackTraverse(SqStack S) //遍历栈,输出路径
{
while(S.top > S.base)
{
*--S.top;
printf("序号: %d 位置:(%d,%d) 方向: %d 足迹:%d \n",
S.top->ord,S.top->curpos.x,S.top->curpos.y,S.top->di,S.top->footprint);
}
printf("\n");
return TRUE;
}
int NextPos(Position curpos,int di) //将当前位置移动到下一位置,继续探索
{
switch(di)
{
case 1: //将当前位置东移
curpos.x ++;
return curpos.x;
break;
case 2: //北移
curpos.y --;
return curpos.y;
break;
case 3: //西移
curpos.x --;
return curpos.x;
break;
case 4: //南移
curpos.y ++;
return curpos.y;
break;
}
//return FALSE;
}
void errormessage() //错误消息提示
{
printf("错误!\n");
}
int MazePath(Position start,Position end)
{ //若迷宫Maze 中存在从入口 start 到出口 end 的通道,则求得一条存放在栈中(从栈底到栈顶),并返回 TRUE ;否则返回 FALSE
int **temp = Maze();
e.curpos.x = map.start.x; //设定"当前位置"为"入口位置"
e.curpos.y = map.start.y;
map.curstep = 1; //探索第一步
do
{
if( Pass(temp, e.curpos) && e.footprint != -1 ) //当前位置可以通过,即是未曾走过的通道块
{
//FootPrint(temp, e.curpos); //留下足迹,已访问
e.footprint = -1;
e.ord = map.curstep;
e.di = 1;
Push(S, e); //加入路径
if(e.curpos.x == map.end.x &&
e.curpos.y == map.end.y) //到达终点(出口)
return TRUE;
e.curpos.x = NextPos(e.curpos, 1); //下一位置是当前位置的东邻
map.curstep ++; //探索下一步
}
else //当前位置不能通过
{
if( !StackEmpty(S) )
{
Pop(S,e); //退回上一步
while(e.di == 4 && !StackEmpty(S))
{
e.footprint = -2;
//MarkPrint(temp, e.curpos); //留下不能通过的标记
Pop(S,e); //退回一步
}
if(e.di < 4)
{
e.di++; //换一个方向探索
Push(S,e);
printf("入栈之后:");StackTraverse(S);
switch(e.di) //设定当前位置是新方向上的相邻块
{
case 1: //东邻
e.curpos.x = NextPos(e.curpos, e.di);
e.footprint = 0;
break;
case 2: //北邻
e.curpos.y = NextPos(e.curpos, e.di);
e.footprint = 0;
break;
case 3: //西邻
e.curpos.x = NextPos(e.curpos, e.di);
e.footprint = 0;
break;
case 4: //南邻
e.curpos.y = NextPos(e.curpos, e.di);
e.footprint = 0;
break;
}//switch
printf("下一位置: (%d,%d) 元素: %d 足迹: %d\n",e.curpos.x,e.curpos.y,temp[e.curpos.y][e.curpos.x],e.footprint);
}//if
}//if
}//else
}while( !StackEmpty(S) );
return FALSE;
}
void PrintMap(int **temp)
{
int i,j;
for(i=0;i<height+2;i++)
{putchar('\n');
for(j=0;j<width+2;j++)
printf("%d ",temp[i][j]);
putchar('\n');
}
}
void menu() //菜单函数
{
int **m = Maze();
int select;
printf("\n\n1:显示地图 2:显示路径\n\n");
printf("输入选择: ");
scanf("%d ",&select);
//getchar();
switch(select)
{
case 1:PrintMap(m);break;
case 2:StackTraverse(S);break;
default: exit(0);
}
}
void main() //主函数
{
//menu();
int **m = Maze();
PrintMap(m); //输出迷宫
InitStack(S); //初始化栈
SElemType *temp = S.top;
//InitGame(Gam); //初始化游戏参数
map.start.x = map.start.y = 1; //初始化游戏参数
map.end.x = map.end.y = height;
map.curstep = 0;
if( MazePath(map.start, map.end) <= 0) //寻找路径
errormessage(); //探索路径失败
}
[解决办法]
大段的代码,没有格式化,没有指出哪里有问题。
LZ还是自己慢慢看吧。
[解决办法]
问题是什么?
[解决办法]
这么贴代码,谁能看啊?
迷宫问题,就用递归呗
[解决办法]
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xs[10000];
int ys[10000];
int i=0,xx,yy;
int fc,bc;
void push(int x,int y) {
xs[i]=x;
ys[i]=y;
if (i<10000-1) {
i=i+1;
} else {
printf("stack overflow!\n");
exit(1);
}
}
void pop(void) {
i=i-1;
xx=xs[i];
yy=ys[i];
}
int check(int x,int y) {
int c;
c=getpixel(x,y); /* 获取当前点的颜色 */
return ((c!=bc)&&(c!=fc));/* 如果颜色为边界色或填充色则不填充 */
}
void seedfilling(int x,int y,int fill_color,int boundary_color) {
fc=fill_color;
bc=boundary_color;
push(x,y);
while (1) {
if (i<=0) return;
pop();
if (check(xx,yy)) {
putpixel(xx, yy, 14);getch(); /* 加上这行显示当前填充状态 */
putpixel(xx, yy, fill_color); /* 画点 */
if (check(xx-1,yy )) push(xx-1,yy );
if (check(xx ,yy+1)) push(xx ,yy+1);
if (check(xx ,yy-1)) push(xx ,yy-1);
if (check(xx+1,yy )) push(xx+1,yy );
/* 去掉下面四句就是四连通 */
if (check(xx-1,yy-1)) push(xx-1,yy-1);
if (check(xx-1,yy+1)) push(xx-1,yy+1);
if (check(xx+1,yy-1)) push(xx+1,yy-1);
if (check(xx+1,yy+1)) push(xx+1,yy+1);
}
}
}
void main() {
int a,b,color;
int gdriver = DETECT, gmode, errorcode;
int poly[10];
initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
a=150;
b=140;
color=4;
poly[0]=110;/* 第一个点的x坐标以及y坐标 */
poly[1]=110;
poly[2]=200;/* 第二点 */
poly[3]=105;
poly[4]=170;/* 第三点 */
poly[5]=120;
poly[6]=150;/* 第四点 */
poly[7]=170;
poly[8]=110;/* 多边形的起点与终点一样 */
poly[9]=110;
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
/* 保证边界对八连通是封闭的 */
setviewport(0,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,0,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
/* 恢复默认viewport */
setviewport(0,0,600,300,0);
seedfilling(a,b,color,15); /* 种子填充多边形 */
getch();
closegraph();
}