作者在 2010-08-05 21:14:44 发布以下内容
最近编了个迷宫问题的程序,好像除了问题,苦想了很久,还是没能解决,希望那位想挑战一下自己的行家帮忙看看。。。。谢谢了
#include<stdio.h>
#include<malloc.h>
#define Init_way 100
typedef struct point
{
int x;
int y;
int mark;
struct point *south;
struct point *west;
struct point *east;
struct point *north;
}point;
point room[10][10];//迷宫图
typedef struct
{
point *base;
point *top;
int waycount;//计步
}way;
int Initway(way *w)//初始化
{
w->base=(point *)malloc(Init_way*sizeof(point));
if(!w->base)return 0;
else
{
w->top=w->base;
w->waycount=0;
// w->waysize=Init_way;
return 1;
}
}
void push(way *w,point p)//添
{
w->top++;
*(w->top)=p;
}
void pop(way *w)//删
{
#include<malloc.h>
#define Init_way 100
typedef struct point
{
int x;
int y;
int mark;
struct point *south;
struct point *west;
struct point *east;
struct point *north;
}point;
point room[10][10];//迷宫图
typedef struct
{
point *base;
point *top;
int waycount;//计步
}way;
int Initway(way *w)//初始化
{
w->base=(point *)malloc(Init_way*sizeof(point));
if(!w->base)return 0;
else
{
w->top=w->base;
w->waycount=0;
// w->waysize=Init_way;
return 1;
}
}
void push(way *w,point p)//添
{
w->top++;
*(w->top)=p;
}
void pop(way *w)//删
{
w->top--;
w->waycount--;
}
w->waycount--;
}
void print(way *w)
{
int n=1;
for(;n<=w->waycount;n++)
{
w->top=w->base+1;
printf("Step%d:<%d,%d>",n,w->top->x,w->top->y);
}
}
main()
{
int i=0,j=0;
way root;
point *p;
int room1[10][10]={{0,0,0,0,0,0,0,0,0,0},{0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},{0,1,0,0,0,1,1,1,1,0},{0,1,1,1,0,1,1,1,1,0},{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},{0,0,1,1,1,1,1,1,1,0},{0,0,0,0,0,0,0,0,0,0}};
for(;i<10;i++)
for(j=0;j<10;j++)
{
printf("%d ",room1[i][j]);
if(!((j+1)%10))putchar('\n');
}/**/
Initway(&root);
for(i=1;i<9;i++)
{
room[i][0].x=0;
room[i][0].y=i;
room[i][9].x=9;
room[i][9].y=i;
room[i][0].mark=0;
room[i][9].mark=0;
for(j=1;j<9;j++)
{
room[0][j].x=j;
room[0][j].y=0;
room[9][j].x=j;
room[9][j].y=9;
room[0][j].mark=0;
room[0][j].mark=0;
room[i][j].x=j;
room[i][j].y=i;
room[i][j].mark=room1[i][j];
room[i][j].east=&room[i][j+1];
room[i][j].west=&room[i][j-1];
room[i][j].south=&room[i+1][j];
room[i][j].north=&room[i-1][j];
}
}
push(&root,room[1][1]);
do
{
if(root.top->south->mark)
{
(root.top)->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->south;
*(++root.top)=*p;
root.waycount++;
}
else if(root.top->west->mark)
{
root.top->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->west;
*(++root.top)=*p;
root.waycount++;
}
else if(root.top->north->mark)
{
root.top->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->north;
*(++root.top)=*p;
root.waycount++;
}
else if(root.top->east->mark)
{
root.top->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->east;
*(++root.top)=*p;
root.waycount++;
}
else {pop(&root);}
}while(root.top->x!=8||root.top->y!=8);
print(&root);
return 1;
}
{
int n=1;
for(;n<=w->waycount;n++)
{
w->top=w->base+1;
printf("Step%d:<%d,%d>",n,w->top->x,w->top->y);
}
}
main()
{
int i=0,j=0;
way root;
point *p;
int room1[10][10]={{0,0,0,0,0,0,0,0,0,0},{0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},{0,1,0,0,0,1,1,1,1,0},{0,1,1,1,0,1,1,1,1,0},{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},{0,0,1,1,1,1,1,1,1,0},{0,0,0,0,0,0,0,0,0,0}};
for(;i<10;i++)
for(j=0;j<10;j++)
{
printf("%d ",room1[i][j]);
if(!((j+1)%10))putchar('\n');
}/**/
Initway(&root);
for(i=1;i<9;i++)
{
room[i][0].x=0;
room[i][0].y=i;
room[i][9].x=9;
room[i][9].y=i;
room[i][0].mark=0;
room[i][9].mark=0;
for(j=1;j<9;j++)
{
room[0][j].x=j;
room[0][j].y=0;
room[9][j].x=j;
room[9][j].y=9;
room[0][j].mark=0;
room[0][j].mark=0;
room[i][j].x=j;
room[i][j].y=i;
room[i][j].mark=room1[i][j];
room[i][j].east=&room[i][j+1];
room[i][j].west=&room[i][j-1];
room[i][j].south=&room[i+1][j];
room[i][j].north=&room[i-1][j];
}
}
push(&root,room[1][1]);
do
{
if(root.top->south->mark)
{
(root.top)->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->south;
*(++root.top)=*p;
root.waycount++;
}
else if(root.top->west->mark)
{
root.top->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->west;
*(++root.top)=*p;
root.waycount++;
}
else if(root.top->north->mark)
{
root.top->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->north;
*(++root.top)=*p;
root.waycount++;
}
else if(root.top->east->mark)
{
root.top->mark=0;
// room[root.top->y][root.top->x].mark=0;
p=root.top->east;
*(++root.top)=*p;
root.waycount++;
}
else {pop(&root);}
}while(root.top->x!=8||root.top->y!=8);
print(&root);
return 1;
}
是什么问题呢????