Java---Maze

Source: Internet
Author: User

Taking a maze involves a lot of experimentation and errors: go along a path, when you can't backtrack back, and try another option that you haven't tried before. However, a recursive algorithm is used.

masesearch.java//******************************** public class Masesearch {public static void main (string[] args) {Mase labyrinth = new Mase (); System.out.println (Labyrinth); if (Labyrinth.traverse (0,0)) System.out.println ("The Mase was successfully traversed!"); ElseSystem.out.println ("There is no possble path"); System.out.println (Labyrinth);}} mase.java//************************************** public class Mase {private final int tried = 3;private final int PATH = 7;private int[][] grid={{1,1,1 , 0,1,1,0,0,0,1,1,1,1}, {1,0,1,1,1,0,1,1,1,1,0,0,1}, {0,0,0,0,1,0,1,0,1,0,1,0,0}, {1              , 1,1,0,1,1,1,0,1,0,1,1,1}, {1,0,1,0,0,0,0,1,1,1,0,0,1}, {1,0,1,1,1,1,1,1,0,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1,1,1,1,1}};p ublic Boolean TraveRSE (int row,int column) {Boolean done = False;if (valid (Row,column))///Determine if 1, 1 true {Grid[row][column] = tried;if (row = g Rid.length-1 && column = = grid[0].length-1) done = True;else{done = Traverse (Row+1,column), if (!done) done = Traverse (row,column+1); if (!done) done = Traverse (Row-1,column), if (!done) done = Traverse (row,column-1);} if (done) Grid[row][column]=path;} return done;} Private Boolean valid (int row,int column) {boolean result = False;if (row>=0 && row<grid.length && Co Lumn >= 0 && Column < grid[row].length) if (grid[row][column] = = 1) result = True;return result;} Public String toString () {string result = "\ n"; for (int row = 0;row < grid.length;row++) {for (int column=0; Column<grid [row].length;column++) result + = Grid[row][column] + ""; result + = "\ n";} return result;}}

  

  

  

Java---Maze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.