#include "interface.h"
unsigned char map[8][8];
int x=0,y=0;
int i=0,j=0;
int direction=0;
int top=0;
int mx[4]={0,1,0,-1};
int my[4]={1,0,-1,0};
unsigned char stack_x[30]={-1};
unsigned char stack_y[30]={-1}; //堆栈,记录岔路口
void InitStack()
{
stack_x[0]=0; //从起点开始压栈
stack_y[0]=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
map[i][j]=0xFF;
}
}
}
void xunlu()//低四位记录迷宫中每一格所对应的挡板信息;绝对上右下左,0无1有
{
unsigned char dir,sum=0;
if(direction==0) //记录进入的绝对方向
map[x][y]&=0x7f; //上0111
else if(direction==1)
map[x][y]&=0xbf; //右1011
else if(direction==2)
map[x][y]&=0xdf; //下1101
else if(direction==3)
map[x][y]&=0xef; //左1110
if(!(x==0&&y==0)) //初始节点的时候是不记录后方的,其他情况都要记录来的方向
{
dir=(direction+2)%4;
if(dir==0)
map[x][y]&=0xf7; //上0111
else if(dir==1)
map[x][y]&=0xfe; //右1011
else if(dir==2)
map[x][y]&=0xfb; //下1101
else if(dir==3)
map[x][y]&=0xfd; //左1110
}
if(get_ir(3)==1) //记录小车前方是否有墙
{
sum++;
dir=direction;
if(dir==0)
map[x][y]&=0xf7;
else if(dir==1)
map[x][y]&=0xfe;
else if(dir==2)
map[x][y]&=0xfb;
else if(dir==3)
map[x][y]&=0xfd;
}
if(get_ir(5)==1) //右方
{
sum++;
dir=(direction+1)%4;
if(dir==0)
map[x][y]&=0xf7;
else if(dir==1)
map[x][y]&=0xfe;
else if(dir==2)
map[x][y]&=0xfb;
else if(dir==3)
map[x][y]&=0xfd;
}
if(get_ir(1)==1) //左方
{
sum++;
dir=(direction+3)%4;
if(dir==0)
map[x][y]&=0xf7;
else if(dir==1)
map[x][y]&=0xfe;
else if(dir==2)
map[x][y]&=0xfb;
else if(dir==3)
map[x][y]&=0xfd;
}
if(sum>=2) //判断是否为岔路口
{
top++;
stack_x[top]=x;
stack_y[top]=y;
}
}
unsigned int judge(unsigned int k) //判断是否已经走过
{
int s;
k=(k+direction)%4;
s=(x+mx[k])*10+(y+my[k]);
if(s<0)
return 0;
if((0xf0&map[s/10][s%10])==0xf0)
return 1;
else
return 0;
}
void huisu() //高四位记录小车进入迷宫格的方向
{
unsigned char s;
unsigned char dir;
while(1)
{
s=map[x][y]&0xf0;
if(s==0x70)
dir=0;
else if(s==0xb0)
dir=1;
else if(s==0xd0)
dir=2;
else if(s==0xe0)
dir=3;
if(dir==direction) //绝对方向相同
{
go_rel(2);
direction=(direction+2)%4;
x+=mx[direction];
y+=my[direction];
}
else if((dir+direction)%2==0) //绝对方向相反,直接直行
{
x+=mx[direction];
y+=my[direction];
}
else //如有右转左转的情况
{
if(direction==0)
{
if(dir==3) {
go_rel(1);
direction = (direction + 1) % 4;
}
else {
go_rel(3);
direction = (direction + 3) % 4;
}
x+=mx[direction];
y+=my[direction];
}
else if(direction==1)
{
if(dir==0) {
go_rel(1);
direction = (direction + 1) % 4;
}
else {
go_rel(3);
direction = (direction + 3) % 4;
}
x+=mx[direction];
y+=my[direction];
}
else if(direction==2)
{
if(dir==1) {
go_rel(1);
direction = (direction + 1) % 4;
}
else {
go_rel(3);
direction = (direction + 3) % 4;
}
x+=mx[direction];
y+=my[direction];
}
else if(direction==3)
{
if(dir==2) {
go_rel(1);
direction = (direction + 1) % 4;
}
else {
go_rel(3);
direction = (direction + 3) % 4;
}
x+=mx[direction];
y+=my[direction];
}
if(x==stack_x[top]&&y==stack_y[top])
{
top--;
break;
}
}
}
}
void bianli() //右手法则
{
if((0x0f&map[x][y])==0x0f)
xunlu();
if(get_ir(5)==0&&judge(1)) //右边无挡板且未走过
{
go_rel(1);
direction = (direction + 1) % 4;
go_rel(0);
x+=mx[direction];
y+=my[direction];
}
else if(get_ir(3)==0&&judge(0)) //右边有挡板,前边无且未走过
{
go_rel(0);
x+=mx[direction];
y+=my[direction];
}
else if(get_ir(1)==0&&judge(3)) //右边有挡板前边有,左边未走过
{
go_rel(3);
direction = (direction + 3) % 4;
go_rel(0);
x+=mx[direction];
y+=my[direction];
}
else //死胡同
{
huisu();
}
}
int main()
{
InitStack();
while(1)
{
display_mazeInfo();
bianli();
if(x==0&&y==0)
break;
}
}
unsigned char get_ir(unsigned char num); // 返回从左到右1-5红外值
void go_rel(unsigned char relD); // 直行0,右转90°1,掉头2,左转90°3
void display_mazeInfo(); // 0北,1东,2南,3西
你要用递归呀,这样每遇到一个岔路口,可以确保每个岔路都走一遍
你用压栈的方式,等退回到路口的时候,你怎么知道路口已经走过了还是没有走过呢,不陷入死循环了