Home > Back-end >  Using a recursive wrote a 3 x3 small labyrinth, want to know why return can't jump out of the r
Using a recursive wrote a 3 x3 small labyrinth, want to know why return can't jump out of the r

Time:10-03

# include
# include
# include

Int a [3] [3]={
{0, 1},
{0 0},
{0, 0},
};//to build a 3 x3 maze, 2 on behalf of the wall, 1 said starting point

Int iplayer=0;
Int jplayer=0;//start the subscript
Int ci=0;//number of the labyrinth

Void show (int a [3] [3])//display two-dimensional array, is also the current labyrinth
{
Printf (" % d -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - \ n ", + + ci);
for (int i=0; I & lt; 3; I++)
{
For (int j=0; J & lt; 3; J++)
{
Printf (" % 2 d ", a [I] [j]);
}
printf("\n");
}
}

Void AI (int a [3], [3], int, int j)//recursive maze
{
A [I] [j]=3;//path
Show (a);//display the current position
If (I==2 & amp; & J==2)//find the export
{
Printf (" you are win! \n");
return;
}
The else
{
//right, down, left, on
If (j + 1 & lt;=2 & amp; & A [I] [m + 1] <2)//right
{
AI (a, I, j + 1);
}
If (I + 1 & lt;=2 & amp; & A [I + 1] [j] <2)//go down
{
AI (a, I + 1, j);
}
If (j - 1 & gt;=0 & amp; & A [I] [1] <2)//left
{
AI (a, I, j - 1);
}
If (I - 1 & gt;=0 & amp; & A [I - 1) [j] <2)//going up
{
AI (a, I - 1, j);
}
}
}

Void main ()
{
Show (a);
AI (a, iplayer, jplayer);
system("pause");
}

CodePudding user response:

Recursive return just to end the current recursive processing, and then returns to call on a layer of recursive place, if you want to end on a layer of recursion, will be in the previous layer calls the next statement of recursive do judgment, and then exit the recursive,
In this program, you can put the recursive I, j parameters using pointer from spreading way and then the next statement in the calling recursion judgment I, j recursion over,

CodePudding user response:

 void AI (int a [3], [3], int * I, int * j)//change form of pointer 
{
A/* I * [j]=3;//path
Show (a);//display the current position
If (* I==2 & amp; & * j==2)//find the export
{
Printf (" you are win! \n");
return;
}
The else
{
//right, down, left, on
If (* j + 1 & lt;=2 & amp; & A [* I] [* j + 1] <2)//right
{
//AI (a, I, j + 1);
* j +=1;
}
If (* I + 1 & lt;=2 & amp; & I + 1] a [* * [j] <2)//go down
{
//AI (a, I + 1, j);
* I +=1;
}
If (* j - 1 & gt;=0 & amp; & A [* I] [* 1] j & lt; 2)//left
{
//AI (a, I, j - 1);
* j -=1;
}
If (* I - 1 & gt;=0 & amp; & I - 1] a [* * [j] <2)//going up
{
//AI (a, I - 1, j);
* I -=1;
}
AI (a, I, j);//recursive call
If (* I==2 & amp; & * j==2) return;//recursive returns to the ending here and then determine the I, j exit recursive
}
}

CodePudding user response:

After changing a pointer discovered, a careful look at the finish, though, there is no need to change to a pointer because recursive already ended
So your question is called recursive branch useless else if, so after the recursive and into the next if continue recursive
So to else if it is ok to
 void AI (int a [3], [3], int, int j)//recursive maze 
{
A [I] [j]=3;//path
Show (a);//display the current position
If (I==2 & amp; & J==2)//find the export
{
Printf (" you are win! \n");
return;
}
The else
{
//right, down, left, on
If (j + 1 & lt;=2 & amp; & A [I] [m + 1] <2)//right
{
AI (a, I, j + 1);
J +=1;
}
Else if (I + 1 & lt;=2 & amp; & A [I + 1] [j] <2)//go down
{
AI (a, I + 1, j);
}
Else if (j - 1 & gt;=0 & amp; & A [I] [1] <2)//left
{
AI (a, I, j - 1);
}
Else if (I - 1 & gt;=0 & amp; & A [I - 1) [j] <2)//going up
{
AI (a, I - 1, j);
}
}
}

CodePudding user response:

Recursive stack, the return of the current stack, but the former things have to continue, you can imagine this as number, 0 - & gt; 1-> 2, N perform finished is N - 1 executive position, left until completion of the top.

CodePudding user response:

Put up
 if (I - 1 & gt;=0 & amp; & A [I - 1) [j] <2)//going up 
{
AI (a, I - 1, j);
}

Within the else try
 if (I==2 & amp; & J==2)//find the export 
{
Printf (" you are win! \n");
return;
}
The else
{
//right, down, left, on
If (j + 1 & lt;=2 & amp; & A [I] [m + 1] <2)//right
{
AI (a, I, j + 1);
}
If (I + 1 & lt;=2 & amp; & A [I + 1] [j] <2)//go down
{
AI (a, I + 1, j);
}
If (j - 1 & gt;=0 & amp; & A [I] [1] <2)//left
{
AI (a, I, j - 1);
}
If (I - 1 & gt;=0 & amp; & A [I - 1) [j] <2)//going up
{
AI (a, I - 1, j);
}
}
  • Related