小弟初学C语言,写了个寻找迷宫出口的“程序”,写完程序后自己感觉很乱,又无从下手,所以贴上来望高手指点一下。。(改成这样行吗斑竹?)
#include "stdio.h"
#include "stdlib.h"
#define col 10
#define row 10
#define nodes 100
int map[col][row]={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,0,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,1,1,};
typedef struct
{
int x,y;
int dir;
}seater;
typedef struct
{
seater seat[nodes];
int top;
}stack;
seater strpos,endpos,curpos;
stack *router;
void init();
int pass();
void printfoot();
void push();
void nextstep();
void changedir();
void roadprint();
void pop();
void main()
{
init();
do
{
if(pass())
{
printfoot();
push();
nextstep();
}
else
{
curpos=router->seat[router->top];
if(curpos.dir<4)
changedir();
else
pop();
}
if((curpos.x==endpos.x)&&(curpos.y==endpos.y))
{
printf("you get!!\n");
break;
}
}while(router->top>=0);
roadprint();
}
void init ()
{
router=(stack *)malloc(sizeof(stack));
strpos.x=1;
strpos.y=1;
endpos.x=8;
endpos.y=8;
strpos.dir=1;
router->top=-1;
curpos=strpos;
}
int pass()
{
if(map[curpos.x][curpos.y]==0)
return 1;
else
return 0;
}
void printfoot()
{
map[curpos.x][curpos.y]=2;
}
void push()
{
router->top++;
router->seat[router->top]=curpos;
}
void nextstep()
{
curpos=router->seat[router->top];
curpos.y++;
curpos.dir=1;
}
void changedir()
{
router->seat[router->top].dir++;
switch(curpos.dir)
{
case 1:curpos.x++;break;
case 2:curpos.y--;break;
case 3:curpos.x--;break;
case 4:pop();break;
default :printf("error direction!\n");
}
curpos.dir=1;
}
void pop()
{
router->seat[router->top--];
curpos=router->seat[router->top];
changedir();
}
void roadprint()
{
for(int i=0;i<=router->top;i++)
{
printf("seat:%d,%d dir:%d top%d\n",router->seat[i].x,router->seat[i].y,router->seat[i].dir,router->top);
}
}