⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m.txt

📁 自己用c结合数据结构中的栈作的。利用栈实现迷宫的求解。比较感兴趣的可以看看。
💻 TXT
字号:
/*利用栈实现迷宫的求解*/
#include<stdio.h>
#include<stdlib.h>
#define n1 10
#define n2 10
typedef struct node
{
int x;
int y;
int c;
}linkstack;
linkstack top[100];

int maze[n1][n2]={
    1,1,1,1,1,1,1,1,1,1,
    0,0,0,1,0,0,0,1,0,1,
    1,1,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,0,1,
    1,0,0,0,1,0,0,0,0,0,
    1,0,1,0,0,0,1,0,0,1,
    1,0,1,1,1,0,1,1,0,1,
    1,1,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1,};
int i,j,k,m=0;
main()
{
for(i=0;i<n1*n2;i++)
{top[i].c=1;}
printf("the maze is:\n");
for(i=0;i<n1;i++)
{for(j=0;j<n2;j++)
 printf(maze[i][j]?"* ":"  ");
 printf("\n"); }
i=0;top[i].x=1;top[i].y=0;
maze[1][0]=2;
do{
 if(top[i].c<5)
 {if(top[i].x==5 && top[i].y==9)
  {
printf("The way %d is:\n",m++);
   for(j=0;j<=i;j++)
   {printf("(%d,%d)-->",top[j].x,top[j].y);}
   printf("\n");
   for(j=0;j<n1;j++) 
   {for(k=0;k<n2;k++) 
    {if(maze[j][k]==0) printf("  ");
     else if(maze[j][k]==2)  printf("O ");
     else printf("* ");}
    printf("\n"); } 
   maze[top[i].x][top[i].y]=0;
   top[i].c = 1;
   i--;
   top[i].c += 1;
   continue;}
  switch (top[i].c)   
  {
   case 1:
{ if(maze[top[i].x][top[i].y+1]==0)
     {i++;
      top[i].x=top[i-1].x;
      top[i].y=top[i-1].y+1;
      maze[top[i].x][top[i].y]=2;}
     else
     {top[i].c += 1;}
     break;}
   case 2:
    {if(maze[top[i].x-1][top[i].y]==0)
     {i++;
      top[i].x=top[i-1].x-1;
      top[i].y=top[i-1].y;
      maze[top[i].x][top[i].y]=2;}
     else
     {top[i].c += 1;}
     break;}
   case 3:
    {if(maze[top[i].x][top[i].y-1]==0)
     {i++;
      top[i].x=top[i-1].x;
      top[i].y=top[i-1].y-1;
      maze[top[i].x][top[i].y]=2;}
     else
     {top[i].c += 1;}
     break;}
   case 4:
    {if(maze[top[i].x+1][top[i].y]==0)
     {i++;
      top[i].x=top[i-1].x+1;
      top[i].y=top[i-1].y;
      maze[top[i].x][top[i].y]=2;}
     else
     { top[i].c += 1;}
     break;}
  }
 }
 else  
 {if(i==0) return;  
  maze[top[i].x][top[i].y]=0;
  top[i].c = 1;
  i--;
  top[i].c += 1;}
}while(1);
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -