main() /*走迷宫*/
{
int i,g,k,j,xx,yy,gx=5,gy=5,b[100][100];
struct ly
{
int x;
int y;
int d;
} a[100];
b[0][0]=1; b[0][1]=1; b[0][2]=1; b[0][3]=1; b[0][4]=1; b[0][5]=1; b[0][6]=1;
b[1][0]=1; b[1][1]=1; b[1][2]=1; b[1][3]=0; b[1][4]=0; b[1][5]=0; b[1][6]=1;
b[2][0]=1; b[2][1]=1; b[2][2]=0; b[2][3]=1; b[2][4]=0; b[2][5]=1; b[2][6]=1;
b[3][0]=1; b[3][1]=0; b[3][2]=0; b[3][3]=1; b[3][4]=1; b[3][5]=0; b[3][6]=1;
b[4][0]=1; b[4][1]=0; b[4][2]=1; b[4][3]=0; b[4][4]=0; b[4][5]=1; b[4][6]=1;
b[5][0]=1; b[5][1]=0; b[5][2]=0; b[5][3]=0; b[5][4]=1; b[5][5]=0; b[5][6]=1;
b[6][0]=1; b[6][1]=1; b[6][2]=1; b[6][3]=1; b[6][4]=1; b[6][5]=1; b[6][6]=1;
i=1; a[i].x=1; a[i].y=1; a[i].d=1;
while(1)
{
g=1;
for (k=i-1;k>=1;k--)
{
xx=a[i].x-a[k].x;
yy=a[i].y-a[k].y;
if(xx==0 && yy==0)
g=0;
if(a[i].d==9)
g=0;
if(b[a[i].x][a[i].y]==1)
g=0;
}
if ((a[i].x !=gx || a[i].y !=gy) && g==1)
{
i=i+1;
a[i].d=1;
if(a[i-1].d==1)
{ a[i].x=a[i-1].x;
a[i].y=a[i-1].y+1;
}
if(a[i-1].d==2)
{ a[i].x=a[i-1].x+1;
a[i].y=a[i-1].y+1;
}
if(a[i-1].d==3)
{
a[i].x=a[i-1].x+1;
a[i].y=a[i-1].y;
}
if(a[i-1].d==4)
{
a[i].x=a[i-1].x+1;
a[i].y=a[i-1].y-1;
}
if(a[i-1].d==5)
{
a[i].x=a[i-1].x;
a[i].y=a[i-1].y-1;
}
if(a[i-1].d==6)
{
a[i].x=a[i-1].x-1;
a[i].y=a[i-1].y-1;
}
if(a[i-1].d==7)
{
a[i].x=a[i-1].x-1;
a[i].y=a[i-1].y;
}
if(a[i-1].d==8)
{
a[i].x=a[i-1].x-1;
a[i].y=a[i-1].y+1;
}
continue;}
if (g==0)
{
i--;
a[i].d++;
if(i==1 && a[i].d==9) /*这就是迷宫没路可走的判断语句应该加的位置*/
{
printf("mei lu ke zou\n");
break;
}
}
if (a[i].x==gx && a[i].y==gy)
{
for(j=1;j<=i-1;j++)
printf("(%d,%d) --->",a[j].x,a[j].y);
printf("(%d,%d)",a[i].x,a[i].y);
printf("\n");
break;
}
}
scanf("%d",&xx);
}运行结果: (1,1) --->(1,2) --->(1,3) --->(2,2) --->(3,2) --->(4,3) --->(4,4) --->(4,5) --->
(5,5)
0 件のコメント:
コメントを投稿