电子老鼠走迷宫
#include <stdio.h>
#define SIZE 12
int mg[12][12]={0};
/*测试用例
int mg[12][12]={-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,\
-2,0,0,0 ,0 ,0, 0,-2,0,-2,-2,-2,\
-2, 0,-2, 0,-2,-2, 0, 0, 0, 0, 0,-2,\
-2 ,0,-2 ,0,-2,-2 ,0,-2,-2,-2 ,0,-2,\
-2, 0,-2, 0, 0, 0, 0, 0,-2, 0, 0,-2,\
-2, 0,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,\
-2, 0, 0, 0,-2, 0,-2, 0, 0, 0, 0,-2,\
-2, 0,-2,-2,-2, 0, 0, 0,-2,-2,-2,-2,\
-2, 0, 0, 0, 0, 0, -2, 0, 0, 0 ,0,-2,\
-2,-2,-2, 0,-2,-2,-2,-2, 0,-2, 0,-2,\
-2,-2,-2,-2,-2,-2,-2, 0 ,0,-2,-2,-2,\
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2};
*/
enum step{NORTH=-1,\
SOURTH=1,\
WEST=-1,\
EAST=1};//四个方向
typedef struct node
{
int i;//x值
int j;//y值
}Position;
int stepnumber(0);//存储步数
int head(0),tail(0);//分别指向前方和后方
Position start={1,8},end={10,7};
Position pos[60];//存储广度优先的几个节点 队列
//几个函数--
void Output(int mg[][12])
{
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<12;j++)
{
printf("%5d",mg[i][j]);
/*
if(mg[i][j]!=0||mg[i][j]>=10)
printf("%d",mg[i][j]);
else
printf(" %d",mg[i][j]);
*/
}
printf("\n");
}
}
void InitQuene()
{
int i,j;
printf("输入出发点格式(x,y)");
scanf("%d,%d",&i,&j);
start.i=i;
start.j=j;
mg[i][j]=1;
printf("输入终点格式(x,y)");
scanf("%d,%d",&i,&j);
end.i=i;
end.j=j;
head=1;
tail=0;
pos[0]=start;
// mg[1][8]=1;
}
void Createmg(int mg[][12])
{
//建立矩阵---------------------------------------------------------------------------
int j;
char str[12];
//输入矩阵
for(int i=0;i<SIZE;i++)
{
gets(str);//gets()函数非常好
for(j=0;j<12;j++)
{
if(str[j]=='*')
mg[i][j]=-2;
else
mg[i][j]=0;
}
}
//输出矩阵
Output(mg);
//---------------------------------------------------------------------------------
}
//------------------
bool notused(int pos)
{
if(pos!=-2&&pos==0)//必须保证不是墙,并且没有走过
return true;
else
return false;
}
//---------------------
bool IsCompleted(int i,int j)
{
if(i==end.i&&j==end.j)
return true;
else
return false;
}
//-----------------------
void addtoQuene(int i,int j)
{
pos[head].i=i;
pos[head].j=j;
head++;
}
void main()
{
Createmg(mg);
InitQuene();//初始化队列
while(head-tail!=0)//队列非空:head-tail!=0
{
stepnumber++;
Position postemp=pos[tail];
for(int i=1;i<5;i++)
{
int tempi(0),tempj(0);
switch(i)
{
case 1://向南
tempi=postemp.i+1;
tempj=postemp.j;
if(notused(mg[tempi][tempj]))
{
if(IsCompleted(tempi,tempj))
{
printf("计算完毕了用了%d,累坏了,哈哈\n",mg[postemp.i][postemp.j]);
return ;
}
addtoQuene(tempi,tempj);//加入到队列的头部
mg[tempi][tempj]=mg[postemp.i][postemp.j]+1;
}
break;
case 2://向西
tempi=postemp.i;
tempj=postemp.j-1;
if(notused(mg[tempi][tempj]))
{
if(IsCompleted(tempi,tempj))
{
printf("计算完毕了用了%d,累坏了,哈哈\n",mg[postemp.i][postemp.j]);
return ;
}
addtoQuene(tempi,tempj);//加入到队列的头部
mg[tempi][tempj]=mg[postemp.i][postemp.j]+1;
}
break;
case 3://向北
tempi=postemp.i-1;
tempj=postemp.j;
if(notused(mg[tempi][tempj]))
{
if(IsCompleted(tempi,tempj))
{
printf("计算完毕了用了%d,累坏了,哈哈\n",mg[postemp.i][postemp.j]);
return ;
}
addtoQuene(tempi,tempj);//加入到队列的头部
mg[tempi][tempj]=mg[postemp.i][postemp.j]+1;
}
break;
case 4://向东
tempi=postemp.i;
tempj=postemp.j+1;
if(notused(mg[tempi][tempj]))
{
if(IsCompleted(tempi,tempj))
{
printf("计算完毕了用了%d,累坏了,哈哈\n",mg[postemp.i][postemp.j]);
return ;
}
addtoQuene(tempi,tempj);//加入到队列的头部
mg[tempi][tempj]=mg[postemp.i][postemp.j]+1;
}
break;
}//switch end
}//for end
Output(mg);
tail++;
}//while end
if(head-tail==0)
printf("\n晕啊,迷宫设计错误\n");
}
评论
发表评论