我這是用c寫的。你可以看看,希望能幫助到你。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、永登網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、永登網(wǎng)絡(luò)營(yíng)銷、永登企業(yè)策劃、永登品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供永登建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
#include"stdlib.h"
#include"stdio.h"
#define N 50
#define M 50
int X;
int maze[N+2][M+2];
struct point{
int row,col,predecessor;
}queue[512];
int head=0,tail=0;
void shoudong_maze(int m,int n){
int i,j;
printf("\n\n");
printf("請(qǐng)按行輸入迷宮,0表示通路,1表示障礙:\n\n");
for(i=0;im;i++)
for(j=0;jn;j++)
scanf("%d",maze[i][j]);
}
void zidong_maze(int m,int n){
int i,j;
printf("\n迷宮生成中……\n\n");
system("pause");
for(i=0;im;i++)
for(j=0;jn;j++)
maze[i][j]=rand()%2;
//由于rand()產(chǎn)生的隨機(jī)數(shù)是從0到RAND_MAX
//RAND_MAX是定義在stdlib.h中的,其值至少為32767)
//要產(chǎn)生從X到Y(jié)的數(shù),只需要這樣寫:k=rand()%(Y-X+1)+X;
}
void print_maze(int m,int n){
int i,j;
printf("\n迷宮生成結(jié)果如下:\n\n");
printf("迷宮入口\n");
printf("↓");
for(i=0;im;i++)
{printf("\n");
for(j=0;jn;j++)
{if(maze[i][j]==0) printf("□");
if(maze[i][j]==1) printf("■");}
}
printf("→迷宮出口\n");
}
void result_maze(int m,int n)
{ int i,j;
printf("迷宮通路(用☆表示)如下所示:\n\t");
for(i=0;im;i++)
{ printf("\n");
for(j=0;jn;j++)
{if(maze[i][j]==0||maze[i][j]==2) printf("□");
if(maze[i][j]==1) printf("■");
if(maze[i][j]==3) printf("☆");
}
}
}
void enqueue(struct point p)
{ queue[tail]=p;
tail++;
}
struct point dequeue()
{ head++;
return queue[head-1];
}
int is_empty()
{ return head==tail;
}
void visit(int row,int col,int maze[52][52])
{ struct point visit_point={row,col,head-1};
maze[row][col]=2;
enqueue(visit_point);
}
int mgpath(int maze[52][52],int m,int n)
{ X=1;
struct point p={0,0,-1};
if(maze[p.row][p.col]==1)
{ printf("\n===============================================\n");
printf("此迷宮無解\n\n");X=0;return 0;}
maze[p.row][p.col]=2;
enqueue(p);
while(!is_empty())
{p=dequeue();
if((p.row==m-1)(p.col==n-1)) break;
if((p.col+1n)(maze[p.row][p.col+1]==0)) visit(p.row,p.col+1,maze);
if((p.row+1m)(maze[p.row+1][p.col]==0)) visit(p.row+1,p.col,maze);
if((p.col-1=0)(maze[p.row][p.col-1]==0)) visit(p.row,p.col-1,maze);
if((p.row-1=0)(maze[p.row-1][p.col]==0)) visit(p.row-1,p.col,maze);
}
if(p.row==m-1p.col==n-1)
{printf("\n==================================================================\n");
printf("迷宮路徑為:\n");
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
while(p.predecessor!=-1)
{p=queue[p.predecessor];
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
}
}
else {printf("\n=============================================================\n");
printf("此迷宮無解!\n\n");X=0;}
return 0;
}
int main()
{int i,m,n,cycle=0;
while(cycle!=(-1))
{
printf("********************************************************************************\n");
printf(" ☆歡迎進(jìn)入迷宮求解系統(tǒng)☆\n");
printf(" 設(shè)計(jì)者:尹旭 林靜波(信息2班)\n");
printf("********************************************************************************\n");
printf(" 手動(dòng)生成迷宮 請(qǐng)按:1\n");
printf(" 自動(dòng)生成迷宮 請(qǐng)按:2\n");
printf(" 退出 請(qǐng)按:3\n\n");
printf("********************************************************************************\n");
printf("\n");
printf("請(qǐng)選擇你的操作:\n");
scanf("%d",i);
switch(i)
{case 1:printf("\n請(qǐng)輸入行數(shù):");
scanf("%d",m);
printf("\n");
printf("請(qǐng)輸入列數(shù):");
scanf("%d",n);
while((m=0||m50)||(n=0||n50))
{ printf("\n抱歉,你輸入的行列數(shù)超出預(yù)設(shè)范圍(0-50,0-50),請(qǐng)重新輸入:\n\n");
printf("請(qǐng)輸入行數(shù):");
scanf("%d",m);
printf("\n");
printf("請(qǐng)輸入列數(shù):");
scanf("%d",n);
}
shoudong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');
break;
case 2:printf("\n請(qǐng)輸入行數(shù):");
scanf("%d",m);
printf("\n");
printf("請(qǐng)輸入列數(shù):");
scanf("%d",n);
while((m=0||m50)||(n=0||n50))
{printf("\n抱歉,你輸入的行列數(shù)超出預(yù)設(shè)范圍(0-50,0-50),請(qǐng)重新輸入:\n\n");
printf("請(qǐng)輸入行數(shù):");
scanf("%d",m);
printf("\n");
printf("請(qǐng)輸入列數(shù):");
scanf("%d",n);
}
zidong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");getchar();while(getchar()!='\n');break;
case 3:cycle=(-1);
break;
default:printf("\n");
printf("你的輸入有誤!\n");
printf("\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');break;
}
}
}
package 走迷宮;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
// 迷宮
public class Maze extends JFrame implements ActionListener {
private JPanel panel;
private JPanel northPanel;
private JPanel centerPanel;
private MazeGrid grid[][];
private JButton restart;
private JButton dostart;
private int rows;// rows 和cols目前暫定只能是奇數(shù)
private int cols;
private ListString willVisit;
private ListString visited;
private LinkedListString comed;
private long startTime;
private long endTime;
public Maze() {
rows = 25;
cols = 25;
willVisit = new ArrayListString();
visited = new ArrayListString();
comed = new LinkedListString();
init();
this.setTitle("回溯法--走迷宮");
this.add(panel);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
panel = new JPanel();
northPanel = new JPanel();
centerPanel = new JPanel();
panel.setLayout(new BorderLayout());
restart = new JButton("重新生成迷宮");
dostart = new JButton("開始走迷宮");
grid = new MazeGrid[rows][cols];
centerPanel.setLayout(new GridLayout(rows, cols, 1, 1));
centerPanel.setBackground(new Color(0, 0, 0));
northPanel.add(restart);
northPanel.add(dostart);
dostart.addActionListener(this);
restart.addActionListener(this);
for (int i = 0; i grid.length; i++)
for (int j = 0; j grid[i].length; j++) {
if (j % 2 == 0 i % 2 == 0)
grid[i][j] = new MazeGrid(true, 20, 20);
else
grid[i][j] = new MazeGrid(false, 20, 20);
}
grid[0][0].setVisited(true);
grid[0][0].setPersonCome(true);
grid[0][0].setStart(true);
visited.add("0#0");
grid[rows - 1][cols - 1].setEnd(true);
grid = createMap(grid, 0, 0);
for (int i = 0; i grid.length; i++)
for (int j = 0; j grid[i].length; j++) {
grid[i][j].repaint();
centerPanel.add(grid[i][j]);
}
panel.add(northPanel, BorderLayout.NORTH);
panel.add(centerPanel, BorderLayout.CENTER);
}
/**
* 生成迷宮
*
* @param mazeGrid
* @param x
* @param y
* @return
*/
public MazeGrid[][] createMap(MazeGrid mazeGrid[][], int x, int y) {
int visitX = 0;
int visitY = 0;
if (x - 2 = 0) {
if (!mazeGrid[x - 2][y].isVisited()) {
willVisit.add((x - 2) + "#" + y);
}
}
if (x + 2 cols) {
if (!mazeGrid[x + 2][y].isVisited()) {
willVisit.add((x + 2) + "#" + y);
}
}
if (y - 2 = 0) {
if (!mazeGrid[x][y - 2].isVisited()) {
willVisit.add(x + "#" + (y - 2));
}
}
if (y + 2 rows) {
if (!mazeGrid[x][y + 2].isVisited()) {
willVisit.add(x + "#" + (y + 2));
}
}
if (!willVisit.isEmpty()) {
int visit = (int) (Math.random() * willVisit.size());
String id = willVisit.get(visit);
visitX = Integer.parseInt(id.split("#")[0]);
visitY = Integer.parseInt(id.split("#")[1]);
mazeGrid[(visitX + x) / 2][(visitY + y) / 2].setMark(true);
mazeGrid[visitX][visitY].setVisited(true);
if (!visited.contains(id)) {// 將這個(gè)點(diǎn)加到已訪問中去
visited.add(id);
}
willVisit.clear();
createMap(mazeGrid, visitX, visitY);
} else {
if (!visited.isEmpty()) {
String id = visited.remove(visited.size() - 1);// 取出最后一個(gè)元素
visitX = Integer.parseInt(id.split("#")[0]);
visitY = Integer.parseInt(id.split("#")[1]);
mazeGrid[visitX][visitY].setVisited(true);
createMap(mazeGrid, visitX, visitY);
}
}
return mazeGrid;
}
/**
* 走迷宮
*
* @param mazeGrid
* @param x
* @param y
*/
public String goMaze(MazeGrid mazeGrid[][], int x, int y) {
int comeX = 0;
int comeY = 0;
// left
if (x - 1 = 0) {
if (mazeGrid[x - 1][y].isMark()) {
if (!comed.contains((x - 1) + "#" + y))
willVisit.add((x - 1) + "#" + y);
}
}
// right
if (x + 1 cols) {
if (mazeGrid[x + 1][y].isMark()) {
if (!comed.contains((x + 1) + "#" + y))
willVisit.add((x + 1) + "#" + y);
}
}
// up
if (y - 1 = 0) {
if (mazeGrid[x][y - 1].isMark()) {
if (!comed.contains(x + "#" + (y - 1)))
willVisit.add(x + "#" + (y - 1));
}
}
// down
if (y + 1 rows) {
if (mazeGrid[x][y + 1].isMark()) {
if (!comed.contains(x + "#" + (y + 1)))
willVisit.add(x + "#" + (y + 1));
}
}
if (!willVisit.isEmpty()) {
int visit = (int) (Math.random() * willVisit.size());
String id = willVisit.get(visit);
comeX = Integer.parseInt(id.split("#")[0]);
comeY = Integer.parseInt(id.split("#")[1]);
mazeGrid[x][y].setPersonCome(false);
mazeGrid[comeX][comeY].setPersonCome(true);
mazeGrid[x][y].repaint();
mazeGrid[comeX][comeY].repaint();
willVisit.clear();
comed.add(x + "#" + y);
} else {
if (!comed.isEmpty()) {
String id = comed.removeLast();
comeX = Integer.parseInt(id.split("#")[0]);
comeY = Integer.parseInt(id.split("#")[1]);
mazeGrid[x][y].setPersonCome(false);
mazeGrid[comeX][comeY].setPersonCome(true);
mazeGrid[x][y].repaint();
mazeGrid[comeX][comeY].repaint();
comed.addFirst(x + "#" + y);
}
}
return comeX + "#" + comeY;
}
int comeX = 0;
int comeY = 0;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("重新生成迷宮")) {
refreshMap(grid);
} else if (e.getActionCommand().equals("開始走迷宮")) {
startTime = System.currentTimeMillis();
dostart.setVisible(false);
restart.setText("禁止刷新");
int delay = 1000;
int period = 500;// 循環(huán)間隔
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (grid[rows - 1][cols - 1].isPersonCome()) {
endTime = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "已經(jīng)走出迷宮,耗時(shí)"
+ (endTime - startTime) / 1000 + "秒", "消息提示",
JOptionPane.ERROR_MESSAGE);
this.cancel();
restart.setText("重新生成迷宮");
} else {
String id = goMaze(grid, comeX, comeY);
comeX = Integer.parseInt(id.split("#")[0]);
comeY = Integer.parseInt(id.split("#")[1]);
}
}
}, delay, period);
}
}
/**
* 刷新地圖
*/
public void refreshMap(MazeGrid mazeGrid[][]) {
comeX = 0;
comeY = 0;
willVisit.clear();
visited.clear();
comed.clear();
this.remove(panel);
init();
this.add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String args[]) {
long start = System.currentTimeMillis();
new Maze();
long end = System.currentTimeMillis();
System.out.println("使用ArrayList生成迷宮耗時(shí):" + (end - start) + "毫秒");
}
}
非常難。思路:
1、設(shè)老鼠的行進(jìn)路線都是優(yōu)先選擇下-右-上-左。
2、設(shè)老鼠很聰明,走過的路線走撒泡尿,表示鼠大爺?shù)酱艘挥?,我們可以把?shù)組的值改為3,表示走過,但走不通。
3、這是一個(gè)int[8][8]的二位數(shù)組,那么開始位置下標(biāo)是1,1,結(jié)束位置是6,6。行和列分別用、j表示。
4、實(shí)際路線我們可以設(shè)置2表示,我們可以使用遞歸,讓老鼠不斷測(cè)試路線。
5、最后打印數(shù)組,看老鼠的實(shí)際路線。
給你代碼,你給出的那兩個(gè)類,不能滿足,我的需要,我就沒有使用。
你看一下吧。
----------------------------------------
package stackpackage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Maze {
// 上
private static Point directionTop = new Point(-1, 0);
// 下
private static Point directionBottom = new Point(1, 0);
// 左
private static Point directionLeft = new Point(0, -1);
// 右
private static Point directionRight = new Point(0, 1);
private static Point[] directions = { directionTop, directionRight,
directionBottom, directionLeft };
private static boolean isStop = false;
private static int row = 0;
private static int col = 0;
private static Point startPoint = new Point();
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("data.txt");
BufferedReader br = new BufferedReader(fr);
int rowIndex = 1;
int[][] maze = null;
while (br.ready()) {
String line = br.readLine();
Scanner sc = new Scanner(line);
if (rowIndex == 1) {
row = sc.nextInt();
col = sc.nextInt();
maze = new int[row][col];
} else {
if (rowIndex row + 2) {
for (int i = 0; i col; i++) {
maze[rowIndex - 2][i] = sc.nextInt();
}
} else {
startPoint.x = sc.nextInt();
startPoint.y = sc.nextInt();
}
}
rowIndex++;
}
ListPoint route = new ArrayListPoint();
route.add(startPoint);
findNext(startPoint);
puzzle(maze, startPoint, route);
System.out.println(route);
}
private static void puzzle(int[][] maze, Point p, ListPoint route) {
if (isStop) {
return;
}
Point[] nextDirections = p.nextDirections;
for (int i = 0; i nextDirections.length; i++) {
if (isStop) {
return;
}
Point direction = nextDirections[i];
Point newP = new Point(p.x + direction.x, p.y + direction.y);
if (newP.isEffective() maze[newP.x][newP.y] == 0
!route.contains(newP)) {
newP.before = p;
findNext(newP);
route.add(newP);
if (isExit(newP)) {
isStop = true;
break;
}
puzzle(maze, newP, route);
}
}
if (isStop) {
return;
}
route.remove(route.size() - 1);
}
private static void findNext(Point p) {
int index = 0;
Point[] nextDirections = new Point[3];
for (int i = 0; i nextDirections.length; i++) {
nextDirections[i] = new Point(0, 0);
}
for (int i = 0; i directions.length; i++) {
Point direction = directions[i];
Point newP = new Point(p.x + direction.x, p.y + direction.y);
if (newP.isEffective() !newP.equals(p.before) newP.x row
newP.y col) {
nextDirections[index++] = direction;
}
}
p.nextDirections = nextDirections;
}
private static boolean isExit(Point p) {
if (startPoint.equals(p)) {
return false;
}
for (int i = 0; i directions.length; i++) {
Point direction = directions[i];
Point newP = new Point(p.x + direction.x, p.y + direction.y);
if (!newP.equals(p.before)
(newP.x = row || newP.y = col || newP.x 0 || newP.y 0)) {
return true;
}
}
return false;
}
}
class Point {
int x = 0;
int y = 0;
Point[] nextDirections = null;
Point before = null;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "" + x + "," + y + "";
}
public boolean isEffective() {
return x = 0 y = 0;
}
public boolean equals(Object obj) {
return equals((Point) obj);
}
public boolean equals(Point p) {
if (p == null) {
return false;
}
return this.x == p.x this.y == p.y;
}
}
import?java.io.File;
import?java.io.FileNotFoundException;
import?java.util.HashSet;
import?java.util.LinkedList;
import?java.util.List;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?throws?FileNotFoundException?{
Scanner?input?=?new?Scanner(new?File("Maze2tong.txt"));
int?row?=?0;
char[][]?Mazemap?=?new?char[12][58];
while?(input.hasNext())?{
String?line?=?input.nextLine();
for?(int?column?=?0;?column?=?line.length()?-?1;?column++)?{
char?c?=?line.charAt(column);
Mazemap[row][column]?=?c;
}
row++;
}
for?(int?i?=?0;?i??12;?i++)?{
for?(int?j?=?0;?j??58;?j++)?{
System.out.print(Mazemap[i][j]);
}
System.out.print("\n");
}
LinkedListTwoTupleInteger,?Integer?trace?=?new?LinkedListTwoTupleInteger,?Integer();
System.out.println(maze(Mazemap,?trace));
System.out.println(trace);
}
public?static?boolean?maze(char[][]?maze,
ListTwoTupleInteger,?Integer?trace)?{
LinkedListTwoTupleInteger,?Integer?path?=?new?LinkedListTwoTupleInteger,?Integer();
HashSetTwoTupleInteger,?Integer?traverse?=?new?HashSetTwoTupleInteger,?Integer();
for?(int?i?=?0;?i??maze.length;?i++)?{
for?(int?j?=?0;?j??maze[i].length;?j++)?{
if?(maze[i][j]?==?'S')?{
path.add(new?TwoTupleInteger,?Integer(i,?j));
}
}
}
while?(!path.isEmpty())?{
TwoTupleInteger,?Integer?temp?=?path.pop();
if?(traverse.contains(temp))?{
continue;
}?else?if?(maze[temp.first][temp.second]?==?'F')?{
trace.add(temp);
return?true;
}?else?if?(!traverse.contains(temp))?{
if?(temp.second?+?1??maze[temp.first].length
?maze[temp.first][temp.second?+?1]?!=?'W')
path.add(new?TwoTupleInteger,?Integer(temp.first,
temp.second?+?1));
if?(temp.second?-?1??0
?maze[temp.first][temp.second?-?1]?!=?'W')
path.add(new?TwoTupleInteger,?Integer(temp.first,
temp.second?-?1));
if?(temp.first?+?1??maze.length
?maze[temp.first?+?1][temp.second]?!=?'W')
path.add(new?TwoTupleInteger,?Integer(temp.first?+?1,
temp.second));
if?(temp.first?-?1??0
?maze[temp.first?-?1][temp.second]?!=?'W')
path.add(new?TwoTupleInteger,?Integer(temp.first?-?1,
temp.second));
traverse.add(temp);
trace.add(temp);
}
}
trace.clear();
return?false;
}
}
class?TwoTupleA,?B?{
public?final?A?first;
public?final?B?second;
public?TwoTuple(A?a,?B?b)?{
first?=?a;
second?=?b;
}
@Override
public?int?hashCode()?{
return?first.hashCode()?+?second.hashCode();
}
@Override
public?boolean?equals(Object?obj)?{
if?(!(obj?instanceof?TwoTuple))?{
}
return?obj?instanceof?TwoTuple??first.equals(((TwoTuple)?obj).first)
?second.equals(((TwoTuple)?obj).second);
}
public?String?toString()?{
return?"("?+?first?+?",?"?+?second?+?")";
}
}?//?/:-
import?java.io.File;
import?java.io.FileNotFoundException;
import?java.util.LinkedList;
import?java.util.Scanner;
class?MyPoint
{
public?boolean?visited=false;
public?int?parentRow=-1;
public?int?parentColumn=-1;
public?final?char?content;
public?int?x;
public?int?y;
public?MyPoint(char?c,int?x,int?y)
{
this.content?=?c;
this.x?=?x;
this.y?=?y;
}
}
public?class?Maze
{
public?static?MyPoint[][]?getMazeArray(){
Scanner?input?=?null;
MyPoint[][]?mazemap?=?new?MyPoint[12][58];
try?{
input?=?new?Scanner(new?File("Maze2tong.txt"));
int?row?=?0;
while?(input.hasNext())?{
String?line?=?input.nextLine();
for?(int?column?=?0;?column?=?line.length()?-?1;?column++)?{
char?c?=?line.charAt(column);
MyPoint?point?=?new?MyPoint(c,row,column);
mazemap[row][column]?=?point;
}
row++;
}
input.close();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}
return?mazemap;
}
public?static?boolean?tomRun(MyPoint[][]?maze,MyPoint?end)
{
int?x?=?maze.length;
int?y?=?maze[0].length;
LinkedListMyPoint?stack=new?LinkedListMyPoint();
for?(int?i?=?0;?i??maze.length;?i++)?{
for?(int?j?=?0;?j??maze[i].length;?j++)?{
if?(maze[i][j].content?==?'S')?{
stack.push(maze[i][j]);
maze[i][j].visited=true;
}
}
}
boolean?result=false;
while(!stack.isEmpty())
{
MyPoint?t=stack.pop();
//System.out.println("pop?point:"+t.x+"?"+t.y+"?value:"+maze[t.x][t.y]);
if(t.content?==?'F')
{
result=true;
end.x?=?t.x;
end.y?=?t.y;
break;
}
if(t.x-1??0??maze[t.x-1][t.y].visited==false??maze[t.x-1][t.y].content?!=?'W')
{
stack.push(maze[t.x-1][t.y]);
maze[t.x-1][t.y].parentRow=t.x;
maze[t.x-1][t.y].parentColumn=t.y;
maze[t.x-1][t.y].visited=true;
}
if(t.x?+?1??x??maze[t.x+1][t.y].visited==false??maze[t.x+1][t.y].content?!=?'W')
{
stack.push(maze[t.x+1][t.y]);
maze[t.x+1][t.y].parentRow=t.x;
maze[t.x+1][t.y].parentColumn=t.y;
maze[t.x+1][t.y].visited=true;
}
if(t.y?-?1??0??maze[t.x][t.y?-?1].visited==false??maze[t.x][t.y-1].content?!=?'W')
{
stack.push(maze[t.x][t.y-1]);
maze[t.x][t.y-1].parentRow=t.x;
maze[t.x][t.y-1].parentColumn=t.y;
maze[t.x][t.y-1].visited=true;
}
if(?t.y?+?1??y??maze[t.x][t.y?+?1].visited==false??maze[t.x][t.y+1].content?!=?'W')
{
stack.push(maze[t.x][t.y+1]);
maze[t.x][t.y+1].parentRow=t.x;
maze[t.x][t.y+1].parentColumn=t.y;
maze[t.x][t.y+1].visited=true;
}
}
return?result;
}
public?static?void?show(int?x,int?y,MyPoint[][]?visited)
{
if(visited[x][y].parentRow==-1)
{
System.out.println("["+x+","+y+"]");
return;
}
show(visited[x][y].parentRow,visited[x][y].parentColumn,visited);
System.out.println("-"+"["+x+","+y+"]");
}
public?static?void?main(String[]?args)
{
MyPoint[][]?maze?=?getMazeArray();
MyPoint?point?=?new?MyPoint('c',1,1);
if(tomRun(maze,point))
{
System.out.println("逃生路徑如下:");
show(point.x,point.y,maze);
}
else
System.out.println("無法走出迷宮!");
}
}
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOOOOOOOOOOOOOOWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW