/*
* maze.c
*
* Created on: 2011-12-19
* Author: ailin
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "stack.h"
int maze_path[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,1,1},
{1,0,0,0,1,0,0,0,1,1},
{1,0,1,0,0,0,1,0,0,1},
{1,1,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
static const Pos InPos={1,1};
static const Pos OutPos={8,8};
void maze_algorithem(int step,Pos curenPos,Stack *s,Element m)
{
getch();
do
{
if(IsPath(curenPos))
{
m.sno=step;
m.dir=0;
m.coordinate=curenPos;
Push(s,&m);
maze_path[curenPos.x][curenPos.y]=2;
if(curenPos.x==OutPos.x && curenPos.y == OutPos.y)
{
PrintMaze(curenPos);
printf("OK\n");
break;
}
PrintMaze(curenPos);
getch();
curenPos= NextPos(curenPos,0);
step++;
}else
{
Pop(s,&m);
while(4==m.dir && !IsEmpty(s))
{
maze_path[curenPos.x][curenPos.y]=3;
Pop(s,&m);
}
if(m.dir < 3)
{
m.dir++;
Push(s,&m);
curenPos=NextPos(m.coordinate,m.dir);
}
}
}while(!IsEmpty(s));
}
int IsPath(Pos curenPos)
{
return maze_path[curenPos.x][curenPos.y] == 0 ? 1:0;
}
Pos NextPos(Pos curenPos, int dir)
{
Pos p = curenPos;
switch(dir)
{
case 0:p.x--;
break;
case 1:p.y++;
break;
case 2:p.x++;
break;
case 3:p.y--;
break;
}
return p;
}
void PrintMaze(Pos curenPos)
{
int i,j;
system("cls");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(curenPos.x==j && curenPos.y==i)
{
printf("@");
}
else if(1==maze_path[i][j])
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
评论0
最新资源