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);
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);
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 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()) {
tail = (tail + 1) % size;
array[tail] = node;
len++;
} 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 + ")";
typedef struct{
int Col,Row;//迷宫的大小
int arr[Rangle][Rangle]; //0表示障碍,1表示是可走的通道,-1表示外界的围墙
}MazeType;
void InitMaze(MazeType
M,int col,int row)
{
//按照用户的输入的行数row和列数col列的二维数组(元素值为1或0)
//设置迷宫的初值,加上边缘的一圈的值
void PrintMaze(MazeType M)
//根据已经进行二维数组的标记值来输出迷宫(或者其通路)
bool Pass(MazeType M,PosType pos)
{//求解迷宫M中,从Start到end的一条路径
//若存在则返回true,否则返回false
Stack S;
InitStack(S);
PosType curpos=start;//设置当前坐标为入口位置;
int curstep=1;
//当前的步数
bool Find=false;
//是否找到出口
ElemType e;
do{
if(Pass(M,curpos))
e.step=1;
e.seat=curpos;
e.di=1;//初始化为向右边位置移动
Push(S,e);
if(curpos.c==end.ccurpos.r==end.r)//如果找到了出口则终止,并返回true
Find=true;
return Find;
else{
curpos=NextPos(curpos,1);
curstep++;
else{//当前位置不能通过
if(!StackEmpty(S)){
Pop(S,e);//将已经走过的最近位置弹出,数据保存在e中
MarkPrint(M,e.seat);//留下不能通过的标记
Pop(S,e);
curstep--;
}//while
e.di++;//方向顺时针改变一下
curpos = NextPos(e.seat,e.di); //求下一个节点
}while(!StackEmpty(S)!Find);
//(!StackEmpty(S)!Find);//当栈不为空且没有找到出口
return
false;//没有找到出口则返回false
我这是用c写的.你可以看看,希望能帮助到你.
#include"stdlib.h"
#include"stdio.h"
int X;
struct point{
int row,col,predecessor;
int head=0,tail=0;
void shoudong_maze(int m,int n){
int i,j;
printf("\n\n");
printf("请按行输入迷宫,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){
printf("\n迷宫生成中......\n\n");
system("pause");
//由于rand()产生的随机数是从0到RAND_MAX
//要产生从X到Y的数,只需要这样写:k=rand()%(Y-X+1)+X;
void print_maze(int m,int n){
printf("\n迷宫生成结果如下:\n\n");
printf("迷宫入口\n");
printf("↓");
{printf("\n");
{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");
{ printf("\n");
if(maze[i][j]==1) printf("■");
void enqueue(struct point p)
{ queue[tail]=p;
tail++;
struct point dequeue()
{ head++;
return queue[head-1];
int is_empty()
{ return head==tail;
{ struct point visit_point={row,col,head-1};
enqueue(visit_point);
{ 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;}
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);
while(p.predecessor!=-1)
{p=queue[p.predecessor];
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(" ☆欢迎进入迷宫求解系统☆\n");
printf(" 手动生成迷宫 请按:1\n");
printf("\n");
printf("请选择你的操作:\n");
scanf("%d",i);
switch(i)
{case 1:printf("\n请输入行数:");
scanf("%d",m);
printf("请输入列数:");
scanf("%d",n);
printf("请输入行数:");
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;
zidong_maze(m,n);
printf("\n\nPress Enter Contiue!\n");getchar();while(getchar()!='\n');break;
default:printf("\n");
printf("你的输入有误!\n");
printf("\nPress Enter Contiue!\n");
while(getchar()!='\n');break;
以上就是土嘎嘎小编为大家整理的迷宫java源代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!