因此,全局变量较多,及栈采用数组,函数采用无参数
形式
迷宫采用ASCII字符制表符画成
运行查看: 1。编译 => 生成程序 => TEST.exe
2。在dos下或windos CMD 下,英文模式下运行(汉字状态
下是乱码)
或者
TEST.EXE > 1.doc 再用word (编码:OEM美国)打开
1.doc察看效果(行距定为0)
*/
#include<stdlib.h>
#include<stdio.h>?
#include<time.h>
#define DOWN 0
#define LEFT 1
#define UP 2
#define RIGHT 3
#define L_X 40
#define L_Y 20
unsigned char LabYrinth[L_Y][L_X];
//迷宫
int ROBOT_X,ROBOT_Y ;
//挖洞机器人方位
int Stack[L_X*L_Y*2];
// 记录挖洞机器人路径
int StackTop=0 ;
//栈顶
int OutLabYrinth=0 ;
//找到出口标记
void InitLabYrinth(void);
//初始化迷宫
void printfLabYrinth(void);
//打印迷宫
//测试访方向是否越界
int RobotDirectionInspection(int Direction);
//测试NextY,NextX是否被打通
int RobotChannelInspection(int NextX,int NextY);
//向下打通
void Downward(void);
//向上打通
void upward(void);
// 向右打通
void right(int updown);
//向左打通
void left(int updown);
void RobotMain(void);
//挖洞机器人
void Push(void);
//机器人坐标进栈
void Pop(void);
//机器人坐标出栈
int Robot(int Direction);
int main(void)
{
time_t t ;
srand((unsigned)time(&t));
//随机数初始化
InitLabYrinth();
//初始化迷宫
ROBOT_X=1,ROBOT_Y=0 ;
//挖洞机器人入位
Push();
// 挖洞机器人方位进栈
RobotMain();
// 挖洞
printfLabYrinth();
//输出迷宫
return 0 ;
}
void InitLabYrinth(void)
{
int i,j ;
for(i=1;i<L_Y-1;i++)
for(j=1;j<L_X-1;j++)
{
LabYrinth[i][j]=197 ;
}
for(j=1;j<L_X-1;j++)
{
LabYrinth[0][j]=194 ;
LabYrinth[L_Y-1][j]=193 ;
}
for(i=1;i<L_Y-1;i++)
{
LabYrinth[i][0]=195 ;
LabYrinth[i][L_X-1]=180 ;
}
LabYrinth[0][0]=218 ;
LabYrinth[0][1]=32 ;
//进口
LabYrinth[L_Y-1][0]=192 ;
LabYrinth[0][L_X-1]=191 ;
LabYrinth[L_Y-1][L_X-1]=217 ;
LabYrinth[L_Y-1][L_X-2]=32 ;
//出口
}
void printfLabYrinth(void)
{
int i,j ;
for(i=0;i<L_Y;i++)
{
for(j=0;j<L_X;j++)
{
putchar(LabYrinth[i][j]);
}
putchar(10);
}
}
//测试访方向是否越界
int RobotDirectionInspection(int Direction)
{
switch(Direction)
{
case DOWN :
if(ROBOT_Y+1>=L_Y-1)return 0 ;
break ;
case LEFT :
if(ROBOT_X-1<0)return 0 ;
break ;
case UP :
if(ROBOT_Y-1<0)return 0 ;
break ;
case RIGHT :
if(ROBOT_X+1>=L_X-1)return 0 ;
break ;
}
return 1 ;
}
//测试方位为NextY,NextX是否被打通
int RobotChannelInspection(int NextX,int NextY)
{
int Channel ;
Channel=0 ;
if(!OutLabYrinth&&NextX==38&&NextY==18)
{
OutLabYrinth=1 ;
return 1 ;
}
if(LabYrinth[NextY][NextX]==197||
LabYrinth[NextY][NextX]==195||
LabYrinth[NextY][NextX]==194||
LabYrinth[NextY][NextX]==218)Channel++;
if(LabYrinth[NextY][NextX+1]==197||
LabYrinth[NextY][NextX+1]==180||
LabYrinth[NextY][NextX+1]==191||
LabYrinth[NextY][NextX+1]==194)Channel++;
if(LabYrinth[NextY+1][NextX]==192||
LabYrinth[NextY+1][NextX]==193||
LabYrinth[NextY+1][NextX]==195||
LabYrinth[NextY+1][NextX]==197)Channel++;
if(LabYrinth[NextY+1][NextX+1]==180||
LabYrinth[NextY+1][NextX+1]==193||
LabYrinth[NextY+1][NextX+1]==197||
LabYrinth[NextY+1][NextX+1]==217)Channel++;
if(Channel==4)return 1 ;
//封闭区间返回真值
return 0 ;
//未封闭返回假值
}
//向下打通
void Downward(void)
{
switch(LabYrinth[ROBOT_Y+1][ROBOT_X+1])
{
case 180 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=179 ;
break ;
case 191 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=32 ;
break ;
case 193 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=192 ;
break ;
case 194 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=218 ;
break ;
case 196 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=32 ;
break ;
case 197 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=195 ;
break ;
case 217 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=32 ;
break ;
}
switch(LabYrinth[ROBOT_Y+1][ROBOT_X])
{
case 192 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=32 ;
break ;
case 193 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=217 ;
break ;
case 194 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=191 ;
break ;
case 195 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=179 ;
break ;
case 196 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=32 ;
break ;
case 197 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=180 ;
break ;
case 218 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=32 ;
break ;
}
}
//向上打通
void upward(void)
{
ROBOT_Y--;
Downward();
ROBOT_Y++;
}
// 向右打通
void right(int updown)
{
if(updown)
{
switch(LabYrinth[ROBOT_Y][ROBOT_X+1])
{
case 179 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=32 ;
break ;
case 180 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=217 ;
break ;
case 191 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=32 ;
break ;
case 194 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=196 ;
break ;
case 195 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=192 ;
break ;
case 197 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=193 ;
break ;
case 218 :
LabYrinth[ROBOT_Y][ROBOT_X+1]=32 ;
break ;
}
}
else
{
switch(LabYrinth[ROBOT_Y+1][ROBOT_X+1])
{
case 179 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=32 ;
break ;
case 180 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=191 ;
break ;
case 192 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=32 ;
break ;
case 193 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=196 ;
break ;
case 195 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=218 ;
break ;
case 197 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=194 ;
break ;
case 217 :
LabYrinth[ROBOT_Y+1][ROBOT_X+1]=32 ;
break ;
}
}
}
//向左打通
void left(int updown)
{
if(updown)
{
switch(LabYrinth[ROBOT_Y][ROBOT_X])
{
case 179 :
LabYrinth[ROBOT_Y][ROBOT_X]=32 ;
break ;
case 180 :
LabYrinth[ROBOT_Y][ROBOT_X]=217 ;
break ;
case 191 :
LabYrinth[ROBOT_Y][ROBOT_X]=32 ;
break ;
case 194 :
LabYrinth[ROBOT_Y][ROBOT_X]=196 ;
break ;
case 195 :
LabYrinth[ROBOT_Y][ROBOT_X]=192 ;
break ;
case 197 :
LabYrinth[ROBOT_Y][ROBOT_X]=193 ;
break ;
case 218 :
LabYrinth[ROBOT_Y][ROBOT_X]=32 ;
break ;
}
}
else
{
switch(LabYrinth[ROBOT_Y+1][ROBOT_X])
{
case 179 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=32 ;
break ;
case 180 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=191 ;
break ;
case 192 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=32 ;
break ;
case 193 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=196 ;
break ;
case 195 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=218 ;
break ;
case 197 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=194 ;
break ;
case 217 :
LabYrinth[ROBOT_Y+1][ROBOT_X]=32 ;
break ;
}
}
}
//机器人坐标进栈
void Push(void)
{
Stack[StackTop]=ROBOT_X ;
StackTop++;
Stack[StackTop]=ROBOT_Y ;
StackTop++;
}
//机器人坐标出栈
void Pop(void)
{
if(StackTop<2)return ;
StackTop--;
ROBOT_Y=Stack[StackTop];
StackTop--;
ROBOT_X=Stack[StackTop];
}
void RobotMain()
{
int Direction ;
int Flag=0,i ;
while(1)
{
/*||(ROBOT_X==38&&ROBOT_Y==18)*/
if(StackTop==0)return ;
Direction=rand()%4 ;
Flag=0 ;
for(i=0;i<4;i++)
{
if(Robot(Direction%4))Direction++;
else
{
Flag=1 ;
break ;
}
}
if(!Flag)Pop();
}
}
int Robot(int Direction)
{
if(RobotDirectionInspection(Direction))
{
switch(Direction)
{
case DOWN :
if(RobotChannelInspection(ROBOT_X,ROBOT_Y+1))
{
Downward();
ROBOT_Y++;
Push();
}
else return 1 ;
break ;
case LEFT :
if(RobotChannelInspection(ROBOT_X-1,ROBOT_Y))
{
left(rand()%2);
ROBOT_X--;
Push();
}
else return 1 ;
break ;
case UP :
if(RobotChannelInspection(ROBOT_X,ROBOT_Y-1))
{
upward();
ROBOT_Y--;
Push();
}
else return 1 ;
break ;
case RIGHT :
if(RobotChannelInspection(ROBOT_X+1,ROBOT_Y))
{
right(rand()%2);
ROBOT_X++;
Push();
}
else return 1 ;
break ;
}
return 0 ;
}
else return 1 ;
}
其实这个代码只完成了第一步,要把代码也缩成迷宫样还有更多的工作要做。有人想看源码,就先贴了。^_^