標籤:
題目:
要求使用者輸入一個值n作為一個n*n的矩陣大小,然後使用者輸入n行,每行有n個字元,每個字元用空格隔開,其中字元“A”表示起點,字元“B”表示終點,中間尋路有要求,如果當前字元是“+”則下一步必須是字元“-”或者字元“B”,如果當前字元是“-”則下一步必須是字元“+”或者字元“B”,如果當前字元是“A”則下一步是字元“+”字元“-”字元“B”都行。不考慮使用者輸入的其他字元,只有“+”“-”“B”,然後輸出結果,能到達的最小的步數。
代碼:
import java.util.ArrayList;import java.util.Scanner;public class Main{//矩陣的大小static int n;//用於記錄是否走到了終點,true表示到了,false表示沒有到,預設falsestatic boolean flag = false;//用於存放所有的結果static ArrayList<Integer> list = new ArrayList<Integer>();public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();sc.nextLine();String[][] map = Produce();//測試代碼for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){System.out.print(map[i][j]);}System.out.println();}//得到"A"的座標,"B"的座標int[] a = Local(map, "A");int[] b = Local(map, "B");//測試座標是否正確System.out.println(a[0] + " " + a[1]);System.out.println(b[0] + " " + b[1]);//開始移動Move(map, a, b, 0);System.out.println("=========================");if(list.size() < 1){System.out.println("沒有找到路徑");}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("最小路徑為:" + mix);}//測試結束標誌System.out.println("end!");}private static void Move(String[][] m, int[] a, int[] b, int s) {//用於記錄走過的步數int sum = s;String[][] map = m;//表示當前座標int[] local = a;//表示終點座標int[] end = b;MoveUp(map, local, end, sum);System.out.println(flag);//判斷上一步是否到達了終點if(flag){//加入List集合,然後初始化,接著其他方案list.add(sum+1);flag = false;}//重新賦值sum = s;map = m;local = a;end = b;MoveRight(map, local, end, sum);System.out.println(flag);if(flag){//加入List集合,然後初始化,接著其他方案list.add(sum+1);flag = false;}//重新賦值sum = s;map = m;local = a;end = b;MoveDown(map, local, end, sum);System.out.println(flag);if(flag){//加入List集合,然後初始化,接著其他方案list.add(sum+1);flag = false;}//重新賦值sum = s;map = m;local = a;end = b;MoveLeft(map, local, end, sum);System.out.println(flag);if(flag){//加入List集合,然後初始化,接著其他方案list.add(sum+1);flag = false;}}private static void MoveLeft(String[][] map, int[] local, int[] end, int sum) {////重新定義,用於保護現場,避免下一次走錯//String[][] map = m;//int[] local = a;//int[] end = b;//int sum = s;//首先判斷當前的座標能不能向左移動if(local[1] != 0){//判斷是否到了終點if((local[0] == end[0]) && (local[1]-1 == end[1])){//設定到達了終點flag = true;//return;}else{if(map[local[0]][local[1]].equals("A")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[1]--;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("+") && map[local[0]][local[1]-1].equals("-")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[1]--;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("-") && map[local[0]][local[1]-1].equals("+")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[1]--;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}}}}private static void MoveDown(String[][] map, int[] local, int[] end, int sum) {////重新定義,用於保護現場,避免下一次走錯//String[][] map = m;//int[] local = a;//int[] end = b; //int sum = s;//首先判斷當前的座標能不能向下移動if(local[0] != n-1){//判斷是否到了終點if((local[0]+1 == end[0]) && (local[1] == end[1])){//設定到達了終點flag = true;return;}else{if(map[local[0]][local[1]].equals("A")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[0]++;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("+") && map[local[0]+1][local[1]].equals("-")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[0]++;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("-") && map[local[0]+1][local[1]].equals("+")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[0]++;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}}}}private static void MoveRight(String[][] map, int[] local, int[] end, int sum) {////重新定義,用於保護現場,避免下一次走錯//String[][] map = m;//int[] local = a;//int[] end = b; //int sum = s;//首先判斷當前的座標能不能向右移動if(local[1] != n-1){//判斷是否到了終點if((local[0] == end[0]) && (local[1]+1 == end[1])){//設定到達了終點flag = true;return;}else{if(map[local[0]][local[1]].equals("A")){map[local[0]][local[1]] = " ";//改變座標local[1]++;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("+") && map[local[0]][local[1]+1].equals("-")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[1]++;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("-") && map[local[0]][local[1]+1].equals("+")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[1]++;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}}}}private static void MoveUp(String[][] map, int[] local, int[] end, int sum) {////重新定義,用於保護現場,避免下一次走錯//String[][] map = m;//int[] local = a;//int[] end = b; //int sum = s;//首先判斷當前的座標能不能向上移動if(local[0] != 0){//判斷是否到了終點if((local[0]-1 == end[0]) && (local[1] == end[1])){//設定到達了終點flag = true;return;}else{if(map[local[0]][local[1]].equals("A")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[0]--;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("+") && map[local[0]-1][local[1]].equals("-")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[0]--;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}else if(map[local[0]][local[1]].equals("-") && map[local[0]-1][local[1]].equals("+")){//把當前位置置為空白,避免下一次重複走map[local[0]][local[1]] = " ";//改變座標local[0]--;sum++;//步數加1//調用Move函數,接著往下走Move(map, local, end, sum);}}}}//得到str的座標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;}//產生一個n*n的陣列private static String[][] Produce(){Scanner sc = new Scanner(System.in);String[] m = new String[n];String[][] map = new String[n][n];//控制台輸入for(int i = 0; i < n; i++){m[i] = sc.nextLine();}//對輸入的資料進行轉換成去掉空格的for(int i = 0; i < n; i++){map[i] = m[i].split(" ");}return map;}}
2015年6月1日by:champly
Java演算法--尋路