Home > Back-end >  [for] freshmen need c code
[for] freshmen need c code

Time:09-22

] maze game
A, problem description
Design a maze game, the entrance of a given maze, export, if there is a program to show the path of the walk, and finally reached the export, and outputs the labyrinth "successful"; If there is no outlet, the program can also show the process of walking, and finally back to the entrance, and output "back to the entrance,"

Second, tip
1. Define a 2 d array (MxN) says a maze, an array of elements for the "#" or ". ", "#", said the wall of the maze, ", "said can travel corridor, since the entry into the maze, only along the". "stands for the location of the mobile,
MazeGenerator, (2) to define a function used to generate the labyrinth of any size (width and height of an array of input by the user), request generated by the maze of at least one different from the entrance to the exit,
3. Define a recursive function mazeTraverse, used to implement the mazes, said the function parameter should include a maze of two-dimensional array and the maze entrance, marked characters' X 'function has walked the path, and the output state of maze after every move forward one step,
Mazes of a simple algorithm can always go to export (if any), if there is no exports, will return to the starting point, the algorithm specific expression is as follows: the right hand on the right side of the wall and began to walk forward, hands from wall, can always go to export, eventually, of course, there may be a shorter path, but the path can always go to export,)

CodePudding user response:

Fyi:
/* * 
* @ the expand of Title mice mazes to explore
* @ Author Sun Kun
* @ the Date 2013-11-16
* @ At XUST
* @ All Copyright by Sun Kun
*
*/

# include & lt; Iostream>
using namespace std;

Int maze [9] [9]={//initializes the maze, maze in English as "maze"
,2,2,2,2,2,2,2,2 {2},
,0,0,0,0,0,0,0,2 {2},
,0,2,2,0,2,2,0,2 {2},
,0,2,0,0,2,0,0,2 {2},
,0,2,0,2,0,2,0,2 {2},
,0,0,0,0,0,2,0,2 {2},
,2,0,2,2,0,2,2,2 {2},
,0,0,0,0,0,0,0,2 {2},
,2,2,2,2,2,2,2,2 {2}
};

Int force=1, startJ=1;//entry ranks coordinate
Int endI=7, endJ=7;//export ranks coordinate

Void visit (int, int j)//automatic search path
{
Int m, n;

As [I] [j]=1;

If ((I==endI) & amp; & (j)==endJ)
{
Cout & lt; & lt; Endl & lt; & lt; "Display path:" & lt; & lt; endl;
For (m=0; M<9. M++)
{
For (n=0; N<9. N++)
{
If (maze [m] [n]==2)
Cout & lt; & lt; "S";
Else if (maze [m] [n]==1)
Cout & lt; & lt; "♀";
The else
Cout & lt; & lt; "";
}
Cout & lt; & lt; endl;
}
}

If (as [I] [j + 1)==0)
Visit (I, j + 1);
If (maze [j] [I + 1]==0)
Visit (I + 1, j);
If (as [I] [1]==0)
Visit (I, j - 1);
If (maze [I - 1) [j]==0)
Visit (I - 1, j);

As [I] [j]=0;

}

Int main (void)
{
Int I, j;

Cout & lt; & lt; "Display maze:" & lt; & lt; endl;
for(i=0; i<9. I++)
{
for(j=0; j<9. J++)
{
If (as [I] [j]==2)
Cout & lt; & lt; "S";
The else
Cout & lt; & lt; "";
}
Cout & lt; & lt; endl;
}

Visit (force, startJ);

return 0;
}


Not enough use, in http://www.codeproject.com search "maze"

CodePudding user response:

Thank you, thank you!
  • Related