迷宫Java算法代码 java实现迷宫程序

关于Java走迷宫的问题 。我已经有相关代码了,但是我看不懂 。麻烦高手帮忙注释一下,然后再修改点儿 。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目前暂定只能是奇数
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; igrid.length; i)
for (int j = 0; jgrid[i].length; j) {
if (j % 2 == 0i % 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; igrid.length; i)
for (int j = 0; jgrid[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 (x2cols) {
if (!mazeGrid[x2][y].isVisited()) {
willVisit.add((x2)"#"y);
}
}
if (y - 2 = 0) {
if (!mazeGrid[x][y - 2].isVisited()) {
willVisit.add(x"#"(y - 2));
}
}
if (y2rows) {
if (!mazeGrid[x][y2].isVisited()) {
willVisit.add(x"#"(y2));
}
}
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[(visitXx) / 2][(visitYy) / 2].setMark(true);
mazeGrid[visitX][visitY].setVisited(true);
if (!visited.contains(id)) {// 将这个点加到已访问中去
visited.add(id);
}
willVisit.clear();
createMap(mazeGrid, visitX, visitY);
} else {
if (!visited.isEmpty()) {
String id = visited.remove(visited.size() - 1);// 取出最后一个元素
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 (x1cols) {
if (mazeGrid[x1][y].isMark()) {
if (!comed.contains((x1)"#"y))
willVisit.add((x1)"#"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 (y1rows) {
if (mazeGrid[x][y1].isMark()) {
if (!comed.contains(x"#"(y1)))
willVisit.add(x"#"(y1));
}
}
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;// 循环间隔
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, "已经走出迷宫,耗时"
(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生成迷宫耗时:"(end - start)"毫秒");
}
}
java怎么生成迷宫地图//作者:zhongZw
package cn.zhongZw.model;
import java.util.ArrayList;
import java.util.Random;
public class MazeModel {
private int width = 0;
private int height = 0;
private Random rnd = new Random();
public MazeModel() {
this.width = 50; //迷宫宽度
this.height = 50; //迷宫高度
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public MazeModel(int width, int height) {
super();
this.width = width;
this.height = height;
}
public ArrayListMazePointgetMaze() {
ArrayListMazePointmaze = new ArrayListMazePoint();
for (int h = 0; hheight; h) {
for (int w = 0; wwidth; w) {
MazePoint point = new MazePoint(w, h);
maze.add(point);
}
}
return CreateMaze(maze);
}
private ArrayListMazePointCreateMaze(ArrayListMazePointmaze) {
int top = 0;
int x = 0;
int y = 0;
ArrayListMazePointteam = new ArrayListMazePoint();
team.add(maze.get(xy * width));
while (top = 0) {
int[] val = new int[] {
-1, -1, -1, -1
};
int times = 0;
boolean flag = false;
MazePoint pt = (MazePoint) team.get(top);
x = pt.getX();
y = pt.getY();
pt.visted = true;
ro1: while (times4) {
int dir = rnd.nextInt(4);
if (val[dir] == dir)
continue;
else
val[dir] = dir;
switch (dir) {
case 0: // 左边
if ((x - 1) = 0maze.get(x - 1y * width).visted == false) {
maze.get(xy * width).setLeft();
maze.get(x - 1y * width).setRight();
team.add(maze.get(x - 1y * width));
top;
flag = true;
break ro1;
}
break;
case 1: // 右边
if ((x1)widthmaze.get(x1y * width).visted == false) {
maze.get(xy * width).setRight();
maze.get(x1y * width).setLeft();
team.add(maze.get(x1y * width));
top;
flag = true;
break ro1;
}
break;
case 2: // 上边
if ((y - 1) = 0maze.get(x(y - 1) * width).visted == false) {
maze.get(xy * width).setUp();
maze.get(x(y - 1) * width).setDown();
team.add(maze.get(x(y - 1) * width));
top;
flag = true;
break ro1;
}
break;
case 3: // 下边
if ((y1)heightmaze.get(x(y1) * width).visted == false) {
maze.get(xy * width).setDown();
maze.get(x(y1) * width).setUp();
team.add(maze.get(x(y1) * width));
top;
flag = true;
break ro1;
}
break;
}
times= 1;
}
if (!flag) {
team.remove(top);
top -= 1;
}
}
return maze;
}
}
迷宫
[java] view plain copy
//作者:zhongZw
//邮箱:zhong317@126.com
package cn.zhongZw.model;
import java.util.*;
import java.lang.*;
public class MazePoint {
private int left = 0;
private int right = 0;
private int up = 0;
private int down = 0;
private int x;
private int y;
public boolean visted;
public MazePoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getLeft() {
return left;
}
public void setLeft() {
this.left = 1;
}
public int getRight() {
return right;
}
public void setRight() {
this.right = 1;
}
public int getUp() {
return up;
}
public void setUp() {
this.up = 1;
}
public int getDown() {
return down;
}
public void setDown() {
this.down = 1;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
求用java语言寻找走出迷宫路线的算法首先给定一个初始坐标迷宫Java算法代码,然后构建一个容器保存坐标值迷宫Java算法代码,之后进行迭代 , 横坐标 1,或者纵坐标 1,这个顺寻自己定义(四个方向),经过迷宫Java算法代码的“路径”保存在那个容器中,如果遇到死角,以此往回迭代,在容器中将遇到死角的那个坐标删除 。最后找到自己定义的那个迷宫出口坐标 。
用java语言控制小机器人走迷宫的算法?我昨天刚写了个走迷宫的界面(一个初始小球,一个目标小球,随机在界面种生成障碍(迷宫图),然后初始小球移动到目标小球那),不知道是否跟你的想法一样 。用的是回溯法(目前我只知道这个算法走迷宫),你可以查下 。PS:我电脑没联网不能把代码给你…QQ254774042 。
请帮忙用数据结构(java版)的知识解决这道迷宫问题的程序代码 。我这是用c写的 。你可以看看迷宫Java算法代码 , 希望能帮助到你 。
#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("此迷宫无解迷宫Java算法代码!\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);
}
【迷宫Java算法代码 java实现迷宫程序】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实现迷宫程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读