import?java.util.*;
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了永城免費(fèi)建站歡迎大家使用!
import?java.util.regex.*;
class?MyPoint
{
public?boolean?visited=false;
public?int?parentRow=-1;
public?int?parentColumn=-1;
public?int?x;
public?int?y;
public?MyPoint(){}
public?MyPoint(int?x,int?y)
{
this.x=x;
this.y=y;
}
}
class?Maze
{
String[][]?maze;
final?int?WIDTH;
final?int?HEIGHT;
final?int?START_X=1;
final?int?START_Y=1;
final?int?END_X;
final?int?END_Y;
MyPoint[][]?visited;
public?Maze()
{
Scanner?s=new?Scanner(System.in);
WIDTH=s.nextInt();//輸入迷宮寬度
END_Y=WIDTH-2;
HEIGHT=s.nextInt();//輸入迷宮長(zhǎng)度
END_X=HEIGHT-2;
maze=new?String[HEIGHT][];
visited=new?MyPoint[HEIGHT][];
s.nextLine();//清除輸入的回車
for(int?i=0;iHEIGHT;++i)//輸入迷宮的每一行,每一行中的字符之間用空格隔開(kāi)
{
String?temp=s.nextLine();
maze[i]=temp.split("\\s+");
visited[i]=new?MyPoint[WIDTH];
for(int?j=0;jWIDTH;++j)
{
visited[i][j]=new?MyPoint(i,j);
}
}
System.out.println("input?finish.");
for(int?i=0;iHEIGHT;++i)
{
System.out.println(Arrays.toString(maze[i]));
}
}
public?boolean?tomRun()
{
LinkedListMyPoint?stack=new?LinkedListMyPoint();
stack.push(visited[START_X][START_Y]);
visited[START_X][START_Y].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.x==END_Xt.y==END_Y)
{
result=true;
break;
}
if(visited[t.x-1][t.y].visited==false!maze[t.x-1][t.y].equals("W"))
{
stack.push(visited[t.x-1][t.y]);
visited[t.x-1][t.y].parentRow=t.x;
visited[t.x-1][t.y].parentColumn=t.y;
visited[t.x-1][t.y].visited=true;
}
if(visited[t.x+1][t.y].visited==false!maze[t.x+1][t.y].equals("W"))
{
stack.push(visited[t.x+1][t.y]);
visited[t.x+1][t.y].parentRow=t.x;
visited[t.x+1][t.y].parentColumn=t.y;
visited[t.x+1][t.y].visited=true;
}
if(visited[t.x][t.y-1].visited==false!maze[t.x][t.y-1].equals("W"))
{
stack.push(visited[t.x][t.y-1]);
visited[t.x][t.y-1].parentRow=t.x;
visited[t.x][t.y-1].parentColumn=t.y;
visited[t.x][t.y-1].visited=true;
}
if(visited[t.x][t.y+1].visited==false!maze[t.x][t.y+1].equals("W"))
{
stack.push(visited[t.x][t.y+1]);
visited[t.x][t.y+1].parentRow=t.x;
visited[t.x][t.y+1].parentColumn=t.y;
visited[t.x][t.y+1].visited=true;
}
}
return?result;
}
public?void?show(int?x,int?y)
{
if(visited[x][y].parentRow==-1)
{
System.out.println("["+x+","+y+"]");
return;
}
show(visited[x][y].parentRow,visited[x][y].parentColumn);
System.out.println("-"+"["+x+","+y+"]");
}
public?static?void?main(String[]?args)?
{
Maze?m=new?Maze();
if(m.tomRun())
{
System.out.println("逃生路徑如下:");
m.show(m.END_X,m.END_Y);
}
else
System.out.println("無(wú)法走出迷宮!");
}
}
我這是用c寫的。你可以看看,希望能幫助到你。
#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("此迷宮無(wú)解\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("此迷宮無(wú)解!\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;
}
}
}
在Way函數(shù)的if(Maza[Loci][Locj]==0) 語(yǔ)句里面Maza[Loci][Locj]=2;前面加一句
System.out.println("The Maza route is ("+Loci+","+Locj+")");就打印出程序所走路徑的坐標(biāo)了.
public class Maze { private int[][] maze = null;
private int[] xx = { 1, 0, -1, 0 };
private int[] yy = { 0, 1, 0, -1 };
private Queue queue = null; public Maze(int[][] maze) {
this.maze = maze;
queue = new Queue(maze.length * maze.length);
} public void go() {
Point outPt = new Point(maze.length - 1, maze[0].length - 1);
Point curPt = new Point(0, 0);
Node curNode = new Node(curPt, null);
maze[curPt.x][curPt.y] = 2;
queue.entryQ(curNode); while (!queue.isEmpty()) {
curNode = queue.outQ();
for (int i = 0; i xx.length; ++i) {
Point nextPt = new Point();
nextPt.x = (curNode.point).x + xx[i];
nextPt.y = (curNode.point).y + yy[i];
if (check(nextPt)) {
Node nextNode = new Node(nextPt, curNode);
queue.entryQ(nextNode);
maze[nextPt.x][nextPt.y] = 2;
if (nextPt.equals(outPt)) {
java.util.StackNode stack = new java.util.StackNode();
stack.push(nextNode);
while ((curNode = nextNode.previous) != null) {
nextNode = curNode;
stack.push(curNode);
}
System.out.println("A Path is:");
while (!stack.isEmpty()) {
curNode = stack.pop();
System.out.println(curNode.point);
}
return;
}
}
}
}
System.out.println("Non solution!");
} private boolean check(Point p) {
if (p.x 0 || p.x = maze.length || p.y 0 || p.y = maze[0].length) {
return false;
}
if (maze[p.x][p.y] != 0) {
return false;
}
return true;
} public static void main(String[] args) {
int[][] maze = {
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
};
new Maze(maze).go();
} private class Queue { Node[] array = null;
int size = 0;
int len = 0;
int head = 0;
int tail = 0; public Queue(int n) {
array = new Node[n + 1];
size = n + 1;
} public boolean entryQ(Node node) {
if (isFull()) {
return false;
}
tail = (tail + 1) % size;
array[tail] = node;
len++;
return true;
} public Node outQ() {
if (isEmpty()) {
return null;
}
head = (head + 1) % size;
len--;
return array[head];
} public boolean isEmpty() {
return (len == 0 || head == tail) ? true : false;
} public boolean isFull() {
return ((tail + 1) % size == head) ? true : false;
}
} private class Node { Point point = null;
Node previous = null; public Node() {
this(null,null);
} public Node(Point point, Node node) {
this.point = point;
this.previous = node;
}
} private class Point { int x = 0;
int y = 0; public Point() {
this(0, 0);
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public boolean equals(Point p) {
return (x == p.x) (y == p.y);
} @Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}
給你代碼,你給出的那兩個(gè)類,不能滿足,我的需要,我就沒(méi)有使用。
你看一下吧。
----------------------------------------
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;
}
}