以一個(gè)M×N的長(zhǎng)方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設(shè)計(jì)一個(gè)程序,對(duì)任意設(shè)定的迷宮,求出一條從入口到出口的通路,或得出沒(méi)有通路的結(jié)論。
(1) 根據(jù)二維數(shù)組,輸出迷宮的圖形。
(2) 探索迷宮的四個(gè)方向:RIGHT為向右,DOWN向下,LEFT向左,UP向上,輸出從入口到出口的行走路徑。
例子:
左上角(1,1)為入口,右下角(8,9)為出口。
可使用回溯方法,即從入口出發(fā),順著某一個(gè)方向進(jìn)行探索,若能走通,則繼續(xù)往前進(jìn);否則沿著原路退回,換一個(gè)方向繼續(xù)探索,直至出口位置,求得一條通路。假如所有可能的通路都探索到而未能到達(dá)出口,則所設(shè)定的迷宮沒(méi)有通路。
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]; } /* * 構(gòu)造迷宮 */ public void init(){ Scanner scanner = new Scanner(System.in); System.out.println("請(qǐng)輸入迷宮的行數(shù)"); row = scanner.nextInt(); System.out.println("請(qǐng)輸入迷宮的列數(shù)"); col = scanner.nextInt(); System.out.println("請(qǐng)輸入" + 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; } } // 將原始迷宮復(fù)制到新的迷宮中 for(int i = 0; i < row; ++i) { for(int j = 0; j < col; ++j) { temp[i + 1][j + 1] = maze[i][j]; } } // 從左上角開(kāi)始按照順時(shí)針開(kāi)始查詢 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("沒(méi)有路徑"); } 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(); }}
運(yùn)行示例:
請(qǐng)輸入迷宮的行數(shù)
3
請(qǐng)輸入迷宮的列數(shù)
3
請(qǐng)輸入3行3列的迷宮
0 1 1
0 0 1
1 0 0
有路徑
路徑如下:
請(qǐng)輸入迷宮的行數(shù)
9
請(qǐng)輸入迷宮的列數(shù)
8
請(qǐng)輸入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
有路徑
路徑如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選