In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to implement the maze game with Java recursion. The content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. The origin of the problem
The maze experiment is a classical experiment taken from psychology. In this experiment, a mouse was placed through the door of a large unroofed box, and many walls were set up in the box, forming multiple barriers to the direction of travel. The box has only one exit, and a piece of cheese is placed at the exit to attract the rats to find their way through the maze to reach the exit. The experiment was repeated on the same mouse until the mouse went from entrance to exit without taking a wrong step. After many experiments, the mouse finally learned how to walk the maze.
two。 Description of the problem
There is a maze map with some accessible locations and some unreachable locations (obstacles, walls, boundaries). From one position to the next can only be achieved by one step up (or right, or down, or left), from the starting point, how to find a way to the end.
3. Train of thought analysis
Find the exit of the labyrinth by recursively calling methods according to the agreed strategy (compared to the following-> right-> top-> left). Starting from the starting point, there are four choices for each position (top right, bottom left), first choose a direction (choose the priority direction in the agreed order), and if that direction can go on, then go in this direction and switch the current position to the next position. If you can't walk, go in a different direction, and if you can't go in all directions, go back to the previous position. Keep doing this, and if the current location is the end point, exit the program
Using two-dimensional array to simulate maze map
1 represents the wall
0 means that the location is reachable.
2 means to mark the corresponding location of the map every time you walk through it to avoid repetition.
3 means to walk through this position but can't get through.
4. Code implementation
Public class MiGong {public static void main (String [] args) {int [] [] map = new int [8] [8]; / * * initialize the maze * / for (int I = 0; I
< 8; i++) { map[0][i] = 1; map[7][i] = 1; map[i][0] = 1; map[i][7] = 1; } for (int i = 0; i < 4; i++) { map[3][1+i] = 1; map[5][6-i] = 1; } System.out.println("======迷宫形状======"); for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { System.out.print(map[i][j]+" "); } System.out.println(); } getWay(map,1,1); System.out.println("======行走轨迹======"); for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { System.out.print(map[i][j]+" "); } System.out.println(); } } public static boolean getWay(int[][] map,int i,int j){ if (map[6][6] == 2){ //已经到达终点 return true; }else { if (map[i][j] == 0){ //当前位置还没走过 map[i][j] = 2; if (getWay(map,i+1,j)){ //向下走 return true; } if (getWay(map,i,j+1)){ //向右走 return true; } if (getWay(map,i,j-1)){ //向左走 return true; } if (getWay(map,i-1,j)){ //向上走 return true; }else{ map[i][j] = 3; //四边都走不通 return false; } }else{ //如果不是0就说明过不了或者走过了 return false; } } }}5.结果输出 由于按照下->The agreement order of right-> top-> left is used to determine whether the next step can be taken.
The yellow route will go to the right when it reaches position (4p5) when it comes to (1pd2)
When the position is not zero around, that is, none of the four sides can go, then it will fall back to the previous position.
When I went back to (1), I found that it was still unreachable all around. According to this rule, I went back to (4).
The final route is shown in the red line:
There are different walking routes according to different strategies.
The above content is how to implement the maze game with Java recursion. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.