Home > Back-end >  Maze was supposed to display all the paths but this code shows a great god help him
Maze was supposed to display all the paths but this code shows a great god help him

Time:09-18

# include & lt; stdio.h>
# define M 4
# define N 4
# define MaxSize 100

Int X;//tag dispapath run number
Int mg [M + 2] [N + 2)={
,0,0,0,1,1,1,1,1,1,1 {1}, {1}, {1,0,1,0,0,1}
,1,0,0,0,1,,0,0,0,1,1 {1}, {1}, {1,1,1,1,1,1}};

Struct
{
int i;
int j;
Int di;
} [MaxSize] St, the Path [MaxSize];

Int top=1;
Int count=1;
Int minlen=MaxSize;

Void dispapath ()
{
Int k;
Printf (" % 5 d: "count++);
for(k=0; K<=top; K++)
{
Printf (" (% d, % d) ", St [k]. I, St [k]. J);
}
printf("\n");
If (top + 1 & lt; Minlen)
{
for(k=0; K<=top; K++)
The Path [k]=St [k].
Minlen=top + 1;
}
Printf (" % d ", X);
}

Void dispminpath ()
{
Printf (" the shortest path is as follows: \ n ");
Length: printf (" % d \ n ", minlen);
Printf (" path: ");
for(int k=0; K{
Printf (" (% d, % d) ", the Path [k]. I, the Path [k]. J);
}
printf("\n");
}

Void mgpath (int xi, int yi, int xe, int ye)
{
Int I, j, i1, j1, di;
Bool find;
Top++;
St [top] I=xi;
St [top]. J=yi;
St [top] di=1; Mg [xi] [yi]=1;
While (top> 1)
{
I=St [top]. I; J=St [top]. J;
Di=St [top]. Di;
If (I==xe& & J==ye)
{
X++;
Printf (" tag dispapath run times: % d ", X);
printf("\n");
Dispapath ();
[I] [j]=0 mg;
Top=top - 1;
I=St [top]. I; J=St [top]. J;
Di=St [top]. Di;
}
The find=false;
While (di<3 & amp; & ! Find)
{
Di++;
The switch (di)
{
Case 0: i1=I - 1; J1=j; break;
Case 1: i1=I; J1=j + 1; break;
Case 2: i1=I + 1; J1=j; break;
Case 3: i1=I; J1=j - 1; break;

}
If (mg/i1 (j1)==0)
The find=true;
}
If (find)
{
St [top] di=di;
Top++;
St [top] I=i1; St [top]. J=j1;
St [top] di=1;
Mg [i1] [j1]=1;
}
The else
{
Mg/i1 (j1)=0;
Top -;
}
}
Dispminpath ();
}

Int main ()
{
Printf (" all maze path is as follows: \ n ");
Mgpath (1, 1, M, N);
return 1;
}
  • Related