Java實現走迷宮回溯演算法,java走迷宮回溯

來源:互聯網
上載者:User

Java實現走迷宮回溯演算法,java走迷宮回溯

以一個M×N的長方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設計一個程式,對任意設定的迷宮,求出一條從入口到出口的通路,或得出沒有通路的結論。
(1) 根據二維數組,輸出迷宮的圖形。
(2) 探索迷宮的四個方向:RIGHT為向右,DOWN向下,LEFT向左,UP向上,輸出從入口到出口的行走路徑。

例子:
左上方(1,1)為入口,右下角(8,9)為出口。

可使用回溯方法,即從入口出發,順著某一個方向進行探索,若能走通,則繼續往前進;否則沿著原路退回,換一個方向繼續探索,直至出口位置,求得一條通路。假如所有可能的通路都探索到而未能到達出口,則所設定的迷宮沒有通路。

import java.util.*;class Position{  public Position(){  }  public Position(int row, int col){    this.col = col;    this.row = row;  }  public String toString(){    return "(" + row + " ," + col + ")";  }  int row;  int col;}class Maze{  public Maze(){    maze = new int[15][15];    stack = new Stack<Position>();    p = new boolean[15][15];  }  /*   * 構造迷宮   */  public void init(){    Scanner scanner = new Scanner(System.in);    System.out.println("請輸入迷宮的行數");    row = scanner.nextInt();    System.out.println("請輸入迷宮的列數");    col = scanner.nextInt();    System.out.println("請輸入" + row + "行" + col + "列的迷宮");    int temp = 0;    for(int i = 0; i < row; ++i) {      for(int j = 0; j < col; ++j) {        temp = scanner.nextInt();        maze[i][j] = temp;        p[i][j] = false;      }    }  }  /*   * 回溯迷宮,查看是否有出路   */  public void findPath(){    // 給原始迷宮的周圍家一圈圍牆    int temp[][] = new int[row + 2][col + 2];    for(int i = 0; i < row + 2; ++i) {      for(int j = 0; j < col + 2; ++j) {        temp[0][j] = 1;        temp[row + 1][j] = 1;        temp[i][0] = temp[i][col + 1] = 1;      }    }    // 將原始迷宮複製到新的迷宮中    for(int i = 0; i < row; ++i) {      for(int j = 0; j < col; ++j) {        temp[i + 1][j + 1] = maze[i][j];      }    }    // 從左上方開始按照順時針開始查詢    int i = 1;    int j = 1;    p[i][j] = true;    stack.push(new Position(i, j));    while (!stack.empty() && (!(i == (row) && (j == col)))) {      if ((temp[i][j + 1] == 0) && (p[i][j + 1] == false)) {        p[i][j + 1] = true;        stack.push(new Position(i, j + 1));        j++;      } else if ((temp[i + 1][j] == 0) && (p[i + 1][j] == false)) {        p[i + 1][j] = true;        stack.push(new Position(i + 1, j));        i++;      } else if ((temp[i][j - 1] == 0) && (p[i][j - 1] == false)) {        p[i][j - 1] = true;        stack.push(new Position(i, j - 1));        j--;      } else if ((temp[i - 1][j] == 0) && (p[i - 1][j] == false)) {        p[i - 1][j] = true;        stack.push(new Position(i - 1, j));        i--;      } else {        stack.pop();        if(stack.empty()){          break;        }        i = stack.peek().row;        j = stack.peek().col;      }    }    Stack<Position> newPos = new Stack<Position>();    if (stack.empty()) {      System.out.println("沒有路徑");    } else {      System.out.println("有路徑");      System.out.println("路徑如下:");      while (!stack.empty()) {        Position pos = new Position();        pos = stack.pop();        newPos.push(pos);      }    }    /*     * 圖形化輸出路徑     * */    String resault[][]=new String[row+1][col+1];    for(int k=0;k<row;++k){      for(int t=0;t<col;++t){        resault[k][t]=(maze[k][t])+"";      }    }    while (!newPos.empty()) {      Position p1=newPos.pop();      resault[p1.row-1][p1.col-1]="#";    }    for(int k=0;k<row;++k){      for(int t=0;t<col;++t){        System.out.print(resault[k][t]+"\t");      }      System.out.println();    }  }  int maze[][];  private int row = 9;  private int col = 8;  Stack<Position> stack;  boolean p[][] = null;}class hello{  public static void main(String[] args){    Maze demo = new Maze();    demo.init();    demo.findPath();  }}

運行樣本:

請輸入迷宮的行數
3
請輸入迷宮的列數
3
請輸入3行3列的迷宮
0 1 1
0 0 1
1 0 0

有路徑
路徑如下:

請輸入迷宮的行數
9
請輸入迷宮的列數
8
請輸入9行8列的迷宮
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0
0 0 1 0 1 1 0 1
0 1 1 1 0 0 1 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 1
0 1 1 1 1 0 0 1
1 1 0 0 0 1 0 1
1 1 0 0 0 0 0 0

有路徑
路徑如下:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.