Topic:
Requires the user to enter a value of n as a n*n matrix size, and then the user enters n rows, each line has n characters, each character is separated by a space, where the character "A" denotes the starting point, the character "B" means the end, the middle pathfinding is required, if the current character is "+" the next must be the character "- If the current character is "-" then the next step must be the character "+" or the character "B", if the current character is "A" then the next step is the character "+" character "-" character "B" line. Regardless of the other characters entered by the user, only "+" "-" "B", and then output the result, the smallest number of steps that can be reached.
Code:
Import Java.util.arraylist;import the size of the Java.util.scanner;public class main{//matrix static int n;//is used to record whether the end is reached, and true indicates that the False to indicate that the default falsestatic boolean flag = false;//is used to hold all the results of static arraylist<integer> list = new arraylist< Integer> ();p ublic static void Main (string[] args) {Scanner sc = new Scanner (system.in); n = sc.nextint (); Sc.nextline (); string[][] map = produce ();//test code for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {System.out.print (map[i][j]);} System.out.println ();} Get the coordinates of "a", "B" coordinates int[] A = local (map, "a"); int[] B = local (map, "B");//test coordinates are correct System.out.println (A[0] + "+ a[1]); System.out.println (B[0] + "" + b[1]);//Start moving Move (map, a, b, 0); System.out.println ("========================="), if (List.size () < 1) {System.out.println ("no Path Found");} Else{int mix = list.get (0); for (int i = 1; i < list.size (); i++) {if (Mix > List.get (i)) {mix = List.get (i);}} SYSTEM.OUT.PRINTLN ("Minimum Path:" + Mix);} Test End Flag System.out.println ("end! ");} private static void Move (string[][] m, INt[] A, int[] b, int s) {//used to record the number of steps traversed int sum = s; string[][] Map = m;//represents the current coordinate int[] Local = a;//represents the end coordinate int[] end = b; MoveUp (map, local, end, sum); SYSTEM.OUT.PRINTLN (flag);//Determine if the previous step reached the end if (flag) {//Join the list collection, then initialize, then other scheme List.add (sum+1); flag = false;} Re-assignment sum = S;map = M;local = A;end = b; MoveRight (map, local, end, sum); SYSTEM.OUT.PRINTLN (flag), if (flag) {//Join list collection, then initialize, then other scheme List.add (sum+1); flag = false;} Re-assignment sum = S;map = M;local = A;end = b; MoveDown (map, local, end, sum); SYSTEM.OUT.PRINTLN (flag), if (flag) {//Join list collection, then initialize, then other scheme List.add (sum+1); flag = false;} Re-assignment sum = S;map = M;local = A;end = b; MoveLeft (map, local, end, sum); SYSTEM.OUT.PRINTLN (flag), if (flag) {//Join list collection, then initialize, then other scheme List.add (sum+1); flag = false;}} private static void MoveLeft (string[][] map, int[] Local, int[] end, int sum) {////redefined to protect the scene, avoiding the next error//string[][] map = m ;//int[] Local = a;//int[] End = B;//int sum = s;//First determine if the current coordinates can be moved to the left if (local[1]! = 0) {//To determine if the end point if ((local[0] = = end[0]) &am p;& (local[1]-1 = = End[1])) {//set toReached the end flag = True;//return;} Else{if (Map[local[0]][local[1]].equals ("A")) {//place the current position empty to avoid the next repeat map[local[0]][local[1]] = "";//change the coordinate local[1]--; sum++;//Step number plus 1//call the move function, then go down move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("+") && map[local[0]][local[1]-1].equals ("-")) {//place the current position empty, Avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[1]--;sum++;//steps plus 1//Call the move function, then go down the move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("-") && map[local[0]][local[1]-1].equals ("+")) {//place the current position empty, Avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[1]--;sum++;//steps plus 1//Call the move function, then go down move (map, local, end, sum);}}} private static void MoveDown (string[][] map, int[] Local, int[] end, int sum) {////redefined to protect the scene, avoiding the next error//string[][] map = m ;//int[] Local = a;//int[] end = b; int sum = s;//First to determine if the current coordinates can be moved down if (local[0]! = n-1) {//To determine if the end point if ((local[0]+1 = = End[0]) && (local[1] = = End[1])) { The setting has reached the end flag = True;return;} Else{if (Map[local[0]][local[1]].equals ("A")) {//place the current position empty to avoid the next repeat.MAP[LOCAL[0]][LOCAL[1]] = "";//change the coordinates local[0]++;sum++;//steps plus 1//Call the move function, then go down the move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("+") && map[local[0]+1][local[1]].equals ("-")) {//place the current position empty, Avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[0]++;sum++;//steps plus 1//Call the move function, then go down the move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("-") && map[local[0]+1][local[1]].equals ("+")) {//place the current position empty, Avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[0]++;sum++;//steps plus 1//Call the move function, then go down move (map, local, end, sum);}}} private static void MoveRight (string[][] map, int[] Local, int[] end, int sum) {////redefined to protect the scene, avoiding the next error//string[][] map = m;//int[] Local = a;//int[] end = b; int sum = s;//First to determine if the current coordinates can be moved to the right if (local[1]! = n-1) {//To determine if the end point if ((local[0] = = End[0]) && (local[1]+1 = = End[1])) { The setting has reached the end flag = True;return;} Else{if (Map[local[0]][local[1]].equals ("A")) {map[local[0]][local[1]] = "";//change coordinates local[1]++;sum++;//steps plus 1// Call the move function, then go down the move (map, local, end, sum);} Elseif (map[local[0]][local[1]].equals ("+") && map[local[0]][local[1]+1].equals ("-")) {//place the current position empty to avoid the next repeat map[ LOCAL[0]][LOCAL[1]] = "";//change the coordinates local[1]++;sum++;//steps plus 1//Call the move function, then go down the move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("-") && map[local[0]][local[1]+1].equals ("+")) {//place the current position empty, Avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[1]++;sum++;//steps plus 1//Call the move function, then go down move (map, local, end, sum);}}} private static void MoveUp (string[][] map, int[] Local, int[] end, int sum) {////redefined to protect the scene, avoiding the next error//string[][] map = m;/ /int[] Local = a;//int[] end = b; int sum = s;//First to determine if the current coordinates can be moved upward if (local[0]! = 0) {//To determine if the end point if ((local[0]-1 = = End[0]) && (local[1] = = End[1])) {// The setting has reached the end flag = True;return;} Else{if (Map[local[0]][local[1]].equals ("A")) {//place the current position empty to avoid the next repeat map[local[0]][local[1]] = "";//change the coordinate local[0]--; sum++;//Step number plus 1//call the move function, then go down move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("+") && map[local[0]-1][local[1]].equals ("-")) {//Put the currentPosition is empty to avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[0]--;sum++;//steps plus 1//Call the move function, then go down the move (map, local, end, sum);} else if (map[local[0]][local[1]].equals ("-") && map[local[0]-1][local[1]].equals ("+")) {//place the current position empty, Avoid the next repeat map[local[0]][local[1]] = "";//change the coordinates local[0]--;sum++;//steps plus 1//Call the move function, then go down move (map, local, end, sum);}}} Get str coordinates private static int[] Local (string[][] map, String str) {int[] local = new Int[2];for (int i = 0; i < n; i++) {f or (int j = 0; J < N; j + +) {if (Map[i][j].equals (str)) {local[0] = i;local[1] = J;return Local;}}} return local;} Generates an array of n*n private static string[][] produce () {Scanner sc = new Scanner (system.in); String[] m = new String[n]; string[][] Map = new string[n][n];//console input for (int i = 0; i < n; i++) {M[i] = Sc.nextline ();} Converts the input data into a for (int i = 0; i < n; i++) {Map[i] = M[i].split ("") that removes the space;} return map;}}
June 1, 2015by:champly
Java algorithm--pathfinding