This is an improved version.
Wrap the map in a mouse package. It's an exercise on the basics of Java.
This implementation also encountered a number of problems. The main is the stack. The encapsulated mouse class has a member variable i,j that represents the coordinates of the mouse. When the mouse class object M is in the stack, it is always the reference of an object m in the stack. When you make changes to M again, the contents of the stack become the contents of M.
Solution: Write a function, build a mouse class object inside the function Mtemp, assign the member variable of M i,j first to the int variable, a, B. Assign a A, B to Mtemp's i,j. Then put the mtemp into the stack. It's settled.
The following code is attached for reference only:
1 //Map.java2 3 Packagetest;4 5 ImportJava.util.Stack;6 7 Public classMap {8 int[] maze={9{2,2,2,2,2,2,2,2,2},Ten{2,2,2,2,2,2,2,2,2}, One{2,0,0,0,0,0,0,2,2}, A{2,2,0,2,0,2,0,2,2}, -{2,2,0,0,2,0,2,2,2}, -{2,2,2,0,2,0,2,2,2}, the{2,2,0,0,0,0,0,2,2}, -{2,2,2,2,2,2,0,2,2}, -{2,2,2,2,2,2,2,2,2} -};//Add a circle of fences outside the map to prevent the array from overstepping. + - int[] posi={ +{0,0,0,0,0,0,0,0,0}, A{0,0,0,0,0,0,0,0,0}, at{0,0,0,0,0,0,0,0,0}, -{0,0,0,0,0,0,0,0,0}, -{0,0,0,0,0,0,0,0,0}, -{0,0,0,0,0,0,0,0,0}, -{0,0,0,0,0,0,0,0,0}, -{0,0,0,0,0,0,0,0,0}, in{0,0,0,0,0,0,0,0,0} - }; to +Stack<mouse> stack=NewStack<mouse>(); - the voidCreatepush (Mouse m,stack<mouse>stack) * { $Mouse mtemp=NewMouse ();Panax Notoginseng intA=M.geti (); - intb=M.GETJ (); the Mtemp.seti (a); + MTEMP.SETJ (b); A Stack.push (mtemp); the } + - voidFindpath (mouse m) $ { $Posi[m.geti ()][M.GETJ ()]=1; - - Createpush (m,stack); the - while(!stack.empty ())Wuyi { the if(M.geti () ==7&&M.GETJ () ==6) - Break; Wu if(Maze[m.geti ()][M.GETJ () +1]==0&&posi[m.geti ()][M.GETJ () +1]==0)//Explore Right - { AboutPosi[m.geti ()][M.GETJ () +1]=1; $M.SETJ (M.GETJ () +1); - Createpush (m,stack); - //M.SETJ (M.geti () +1); - //Stack.push (M.geti () + "+M.GETJ ()); A } + Else if(Maze[m.geti () +1][M.GETJ ()]==0&&posi[m.geti () +1][M.GETJ ()]==0)//Explore down the { -Posi[m.geti () +1][M.GETJ ()]=1; $M.seti (M.geti () +1); the Createpush (m,stack); the the } the Else if(Maze[m.geti () -1][M.GETJ ()]==0&&posi[m.geti () -1][M.GETJ ()]==0)//Explore up - { inPosi[m.geti () -1][M.GETJ ()]=1; theM.seti (M.geti ()-1); the Createpush (m,stack); About the } the Else if(Maze[m.geti ()][M.GETJ () -1]==0&&posi[m.geti ()][M.GETJ () -1]==0)//Explore left the { +Posi[m.geti ()][M.GETJ () -1]=1; -M.SETJ (M.GETJ ()-1); the Createpush (m,stack);Bayi the } the Else - { -Stack.pop ();//when you go to a dead end, you stack the points in the stack at that location. the if(Stack.empty ()) the { the Break; the } -M.seti (Stack.peek (). Geti ());//Peek () takes the top element of the stack. the M.SETJ (Stack.peek (). GETJ ()); the } the }94 } the the voidPrint (stack<mouse>stack) the {98Stack<mouse> stacktemp=NewStack<mouse>(); AboutMouse mtemp=NewMouse (); - 101 while(!stack.empty ())//stack reversal. 102 {103mtemp=(mouse) Stack.pop ();104 Stacktemp.push (mtemp); the }106 107 while(!stacktemp.empty ())//The inverted stack, one by one, is the mouse's trajectory. 108 {109System.out.print (Stacktemp.peek (). Geti ()-1); theSystem.out.print ("");111System.out.println (Stacktemp.peek (). GETJ ()-1); the Stacktemp.pop ();113 } the } the}
1 //Mouse.java2 3 Packagetest;4 5 Public classMouse {6 7MouseintIintj)8 {9 This. i=i;Ten This. j=J; One } A Mouse () - { - This. i=0; the This. j=0; - } - - voidSetiinta) + { - This. i=A; + } A voidSETJ (intb) at { - This. j=b; - } - - intGeti () - { in returni; - } to intGETJ () + { - returnJ; the } * $ Private inti,j;Panax Notoginseng}
1 //Test.java2 3 Packagetest;4 5 Public classTest {6 7 Public Static voidMain (string[] args) {8 //TODO auto-generated Method Stub9 TenMap maze=Newmap (); OneMouse m=NewMouse (2,1); A - Maze.findpath (m); - Maze.print (maze.stack); the - } - -}
This practice, although very dull, but I have harvested a lot, a little progress.
Java implementation simple two-dimensional maze (2)