Java Algorithm-path finding
Question:
The user is required to enter a value n as the matrix size of a n * n, and then the user inputs n rows, each line has n characters, each character is separated by a space, the character "A" indicates the start point, and the character "B" indicates the end point. There are requirements for intermediate path searching, if the current character is "+", the next step must be the character "-" or the character "B ", if the current character is "-", the next step must be the character "+" or the character "B ", if the current character is "A", the next step is the character "+", "-", and "B. Regardless of other characters entered by the user, only "+" "-" "B" is allowed, and then the result is output, which is the minimum number of steps that can be reached.
Code:
Import java. util. arrayList; import java. util. optional; public class Main {// matrix size static int n; // used to indicate whether the record has reached the end point. true indicates yes, false indicates no. falsestatic boolean flag = false by default; // stores all results in static ArrayList
List = new ArrayList
(); Public static void main (String [] args) {Using SC = new using (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 () ;}// obtain the coordinates of A. The coordinates of B are int [] a = Local (map, A); int [] B = Local (map, B ); // test whether the coordinates are correct. 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 (path not 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 mark System. out. println (end !);} Private static void Move (String [] [] m, int [] a, int [] B, int s) {// used to record the number of walked steps int sum = s; string [] [] map = m; // indicates the current coordinate int [] local = a; // indicates the end coordinate int [] end = B; MoveUp (map, local, end, sum); System. out. println (flag); // determine whether the end point of the previous step has been reached. if (flag) {// Add the List set, initialize it, And then list other solutions. add (sum + 1); flag = false;} // value sum = s; map = m; local = a; end = B; MoveRight (map, local, end, sum); System. out. println (flag); if (flag) {// Add the List set, however Then initialize and list other solutions. add (sum + 1); flag = false;} // value sum = s; map = m; local = a; end = B; MoveDown (map, local, end, sum); System. out. println (flag); if (flag) {// Add the List set, initialize it, And then list other solutions. add (sum + 1); flag = false;} // value sum = s; map = m; local = a; end = B; MoveLeft (map, local, end, sum); System. out. println (flag); if (flag) {// Add the List set, initialize it, And then list other solutions. add (sum + 1); flag = false;} private static void MoveLeft (String [] [] ma P, int [] local, int [] end, int sum) {// redefinition, used to protect the site, avoid the next error. // String [] [] map = m; // int [] local = a; // int [] end = B; // int sum = s; // first, determine whether the current coordinate can be moved to the left if (local [1]! = 0) {// determine whether the end point is reached if (local [0] = end [0]) & (local [1]-1 = end [1]) {// sets flag to true when the end is reached; // return ;} else {if (map [local [0] [local [1]. equals (A) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [1] --; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) down );} else if (map [local [0] [local [1]. equals (+) & map [local [0] [local [1]-1]. equals (-) {// set the current position to null to avoid repeating map [local [0] [local [1] = next time; // change the coordinates of local [1] --; Sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) down );} else if (map [local [0] [local [1]. equals (-) & map [local [0] [local [1]-1]. equals (+) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [1] --; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) ;}}} private static void MoveDown (String [] [] map, int [] local, int [] end, int sum) {// redefine, used to protect the site and avoid the next error. // String [] [] map = m; // int [] local = A; // int [] end = B; // int sum = s; // first, judge whether the current coordinate can be moved down if (local [0]! = N-1) {// determine whether the end point is reached if (local [0] + 1 = end [0]) & (local [1] = end [1]) {// set flag to true; return ;} else {if (map [local [0] [local [1]. equals (A) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [0] ++; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum);} else if (map [local [0] [local [1]. equals (+) & map [local [0] + 1] [local [1]. equals (-) {// set the current position to null to avoid repeating map [local [0] [local [1] = next time; // change the coordinates of local [0] ++; Sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) down );} else if (map [local [0] [local [1]. equals (-) & map [local [0] + 1] [local [1]. equals (+) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [0] ++; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) ;}}} private static void MoveRight (String [] [] map, int [] local, int [] end, int sum) {// redefine, used to protect the site and avoid the next error. // String [] [] map = m; // int [] local = A; // int [] end = B; // int sum = s; // first, judge whether the current coordinate can be moved to the right if (local [1]! = N-1) {// determine whether the end point is reached if (local [0] = end [0]) & (local [1] + 1 = end [1]) {// set flag to true; return ;} else {if (map [local [0] [local [1]. equals (A) {map [local [0] [local [1] =; // change the coordinate local [1] ++; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) down );} else if (map [local [0] [local [1]. equals (+) & map [local [0] [local [1] + 1]. equals (-) {// set the current position to null to avoid repeating map [local [0] [local [1] = next time; // change the coordinates of local [1] ++; sum ++; // step number plus 1 // call Move function, and then Move (map, local, end, sum);} else if (map [local [0] [local [1]. equals (-) & map [local [0] [local [1] + 1]. equals (+) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [1] ++; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) ;}}} private static void MoveUp (String [] [] map, int [] local, int [] end, int sum) {// redefine, used to protect the site and avoid the next error. // String [] [] map = m; // int [] local = a; // int [] end = B; // Int sum = s; // first, judge whether the current coordinate can be moved up. if (local [0]! = 0) {// determine whether the end point is reached if (local [0]-1 = end [0]) & (local [1] = end [1]) {// set flag to true; return ;} else {if (map [local [0] [local [1]. equals (A) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [0] --; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) down );} else if (map [local [0] [local [1]. equals (+) & map [local [0]-1] [local [1]. equals (-) {// set the current position to null to avoid repeating map [local [0] [local [1] = next time; // change the coordinates of local [0] --; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) down );} else if (map [local [0] [local [1]. equals (-) & map [local [0]-1] [local [1]. equals (+) {// set the current position to null to avoid repeated map [local [0] [local [1] = next time; // change the coordinates of local [0] --; sum ++; // step number plus 1 // call the Move function and Move (map, local, end, sum) ;}}}// obtain the str coordinate private static int [] Local (String [] [] map, String str) {int [] local = new int [2]; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (map [I] [j]. equals (str) {local [0] = I; local [1] = j; return local ;}} return local ;} // generate an n * n Array private static String [] [] Produce () {limit SC = new limit (System. in); String [] m = new String [n]; String [] [] map = new String [n] [n]; // enter for (int I = 0; I <n; I ++) {m [I] = SC in the console. nextLine () ;}// convert the input data to for (int I = 0; I <n; I ++) {map [I] = m [I]. split () ;}return map ;}}