「迷宫绘图java」简单的迷宫画图
今天给各位分享迷宫绘图java的知识,其中也会对简单的迷宫画图进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、关于Java走迷宫的问题。我已经有相关代码了,但是我看不懂。麻烦高手帮忙注释一下,然后再修改点儿。
- 2、求走迷宫问题的算法,要求用Java写的?
- 3、求助 java一个二维数组代表迷宫。0代表道路 2表示墙壁。 假设老鼠会从数组[1][0]开始
- 4、求Java关于迷宫的算法(用栈实现)
- 5、java怎么生成迷宫地图
- 6、求大神按我的要求帮我写一个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; 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)) {// 将这个点加到已访问中去
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 (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;// 循环间隔
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写的?
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一个二维数组代表迷宫。0代表道路 2表示墙壁。 假设老鼠会从数组[1][0]开始
这个可以用 堆栈 来完成。
用堆栈的基本思路就是。
设置一个起点A。将 A 入栈 。
从A开始找到第一个可以达到的点B。将 B 入栈 。
如果B无路可走。则在A点处重新换一个可达到的点。否则继续 2-3 。直到达到终点。或者五路可走。
详细的解释,这儿有一篇博文:
求Java关于迷宫的算法(用栈实现)
package com.Albert.LabyringhStack;
public class Point {
int x;
int y;
int direction; //direction指向此点附近的一个点 应该有四个 编号为1 2 3 4
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;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void addDirection(){
this.direction++;
}
public Point() {
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
this.direction = 1;
}
public Point(int x, int y, int direction) {
super();
this.x = x;
this.y = y;
this.direction = direction;
}
}
package com.Albert.LabyringhStack;
import java.util.*;
public class LabyringhStack {
public Point S;
public Point F;
char[][] mazemap;
StackPoint path;
public LabyringhStack() {
}
public LabyringhStack(char[][] ma) { //初始化 存入数组
this.mazemap = new char[ma.length][ma[0].length];
for(int i=0;ima.length;i++){
for(int j=0;jma[0].length;j++){ //mazemap[0]必须有元素 不可为空
this.mazemap[i][j] = ma[i][j];
}
}
S = returnPlace('S');
F = returnPlace('F');
}
public Point returnPlace(char s){ //返回数组中字符的位置
Point point = new Point();
for(int i=0;ithis.mazemap.length;i++){
for(int j=0;jthis.mazemap[0].length;j++){ //mazemap[0]必须有元素 不可为空
if(this.mazemap[i][j]==s)
{ point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
return point;
}
public char returnChar(Point point){
if(point.getX()=0point.getY()=0)
return this.mazemap[point.getX()][point.getY()];
else
return '#';
}
public void replacePlace(Point point, char s){ //更改特定位置处的字符
mazemap[point.getX()][point.getY()] = s;
}
public void printPath(){
StackPoint tempPath = new StackPoint();
while(!path.empty()){ //对栈进行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print("("+tempPath.peek().getX()+","+tempPath.pop().getY()+")");
}
}
public boolean getPath(){ //取得路径的算法 如果有路径就返回真
path = new StackPoint();
S.setDirection(1);
path.push(S);
replacePlace(S, 'X');
while(!path.empty()){
Point nowPoint = path.peek(); //取得当前位置
if(nowPoint.getX()==F.getX()nowPoint.getY()==F.getY()){
//printPath();
return true;
}
Point temp = new Point(); //存放下一个可走的位置
int find = 0; //标志 是否可向下走
while(nowPoint.getDirection()5find==0){
switch(nowPoint.getDirection()){
case 1:temp = new Point(nowPoint.getX(),nowPoint.getY()-1,1); break; //取得当前位置左边的位置
case 2:temp = new Point(nowPoint.getX()+1,nowPoint.getY(),1); break;//取得当前位置下边的位置
case 3:temp = new Point(nowPoint.getX(),nowPoint.getY()+1,1); break;//取得当前位置右边的位置
case 4:temp = new Point(nowPoint.getX()-1,nowPoint.getY(),1); break;//取得当前位置上边的位置
}
nowPoint.addDirection(); //指向下一个需要验证的点
if(returnChar(temp)=='O'||returnChar(temp)=='F') find = 1; //如果能向下走则置为1
}
if(find==1){ //如果可走就进栈
replacePlace(temp, 'X'); //设置成X 防止回走
// printArr();
path.push(temp);
}else{ //如果不可走就退栈
replacePlace(nowPoint, 'O');
path.pop();
}
}
return false;
}
public void printArr(){
for(int i=0;imazemap.length;i++){
for(int j=0;jmazemap[0].length;j++){ //mazemap[0]必须有元素 不可为空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}
}
package com.Albert.LabyringhStack;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] mazemap = {
{'M','M','M','M','M','M','M','M'},
{'M','S','O','O','M','M','M','M'},
{'M','M','M','O','M','M','M','M'},
{'M','M','O','O','O','O','M','M'},
{'M','M','M','M','M','F','M','M'},
{'M','M','M','M','M','M','M','M'},
{'M','M','M','M','M','M','M','M'}
};
LabyringhStack solution = new LabyringhStack(mazemap);
if(solution.getPath()){
System.out.print("迷宫路径如下:");
solution.printPath();
}
else {
System.out.println("没有可走的路");
}
}
}
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 ArrayList MazePoint getMaze() {
ArrayList MazePoint maze = new ArrayList MazePoint ();
for (int h = 0; h height; h++) {
for (int w = 0; w width; w++) {
MazePoint point = new MazePoint(w, h);
maze.add(point);
}
}
return CreateMaze(maze);
}
private ArrayList MazePoint CreateMaze(ArrayList MazePoint maze) {
int top = 0;
int x = 0;
int y = 0;
ArrayList MazePoint team = new ArrayList MazePoint ();
team.add(maze.get(x + y * 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 (times 4) {
int dir = rnd.nextInt(4);
if (val[dir] == dir)
continue;
else
val[dir] = dir;
switch (dir) {
case 0: // 左边
if ((x - 1) = 0 maze.get(x - 1 + y * width).visted == false) {
maze.get(x + y * width).setLeft();
maze.get(x - 1 + y * width).setRight();
team.add(maze.get(x - 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 1: // 右边
if ((x + 1) width maze.get(x + 1 + y * width).visted == false) {
maze.get(x + y * width).setRight();
maze.get(x + 1 + y * width).setLeft();
team.add(maze.get(x + 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 2: // 上边
if ((y - 1) = 0 maze.get(x + (y - 1) * width).visted == false) {
maze.get(x + y * 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 ((y + 1) height maze.get(x + (y + 1) * width).visted == false) {
maze.get(x + y * width).setDown();
maze.get(x + (y + 1) * width).setUp();
team.add(maze.get(x + (y + 1) * 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的信息别忘了在本站进行查找喔。
发布于:2022-11-21,除非注明,否则均为
原创文章,转载请注明出处。