我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java的迷宫问题代码,java的迷宫问题代码是多少

JAVA迷宫问题,怎么输出所走路径的坐标

在Way函数的if(Maza[Loci][Locj]==0) 语句里面Maza[Loci][Locj]=2;前面加一句

站在用户的角度思考问题,与客户深入沟通,找到婺源网站设计与婺源网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站建设、做网站、企业官网、英文网站、手机端网站、网站推广、主机域名、网页空间、企业邮箱。业务覆盖婺源地区。

System.out.println("The Maza route is ("+Loci+","+Locj+")");就打印出程序所走路径的坐标了.

求走迷宫问题的算法,要求用Java写的?

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 + ")";

}

}

}

请帮忙用数据结构(java版)的知识解决这道迷宫问题的程序代码。

我这是用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("请按行输入迷宫,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()产生的随机数是从0到RAND_MAX

//RAND_MAX是定义在stdlib.h中的,其值至少为32767)

//要产生从X到Y的数,只需要这样写:k=rand()%(Y-X+1)+X;

}

void print_maze(int m,int n){

int i,j;

printf("\n迷宫生成结果如下:\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(" ☆欢迎进入迷宫求解系统☆\n");

printf(" 设计者:尹旭 林静波(信息2班)\n");

printf("********************************************************************************\n");

printf(" 手动生成迷宫 请按:1\n");

printf(" 自动生成迷宫 请按:2\n");

printf(" 退出 请按:3\n\n");

printf("********************************************************************************\n");

printf("\n");

printf("请选择你的操作:\n");

scanf("%d",i);

switch(i)

{case 1:printf("\n请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

scanf("%d",n);

while((m=0||m50)||(n=0||n50))

{ printf("\n抱歉,你输入的行列数超出预设范围(0-50,0-50),请重新输入:\n\n");

printf("请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

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请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

scanf("%d",n);

while((m=0||m50)||(n=0||n50))

{printf("\n抱歉,你输入的行列数超出预设范围(0-50,0-50),请重新输入:\n\n");

printf("请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

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;

}

}

}


网站题目:java的迷宫问题代码,java的迷宫问题代码是多少
网址分享:http://shouzuofang.com/article/hsoheo.html

其他资讯