资源简介
java实现迷宫的自动求解,bfs算法,实现了可视化,有兴趣可以完善监听事件
代码片段和文件信息
package com.zjz.Queue;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Deque;
import java.util.linkedList;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.Jframe;
import javax.swing.JPanel;
public class BFS extends JPanel {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
BFS bfs = new BFS();
Point start = new Point(0 0);
Point dest = new Point(29 29);
bfs.search2(start dest);
System.out.print(“搜索路径:“);
System.out.println(S2);
// for (int[] i : bfs.wall) {
// System.out.println(Arrays.toString(i));
// }
Jframe frame = new Jframe(“迷宫“);
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(500 500);
BFS pane = new BFS();
frame.add(pane);
frame.setVisible(true);
pane.action();
}
public static final int CELL_WIDTH = 12;
/**
*
*/
public void paint(Graphics g) {
g.clearRect(0 0 getWidth() getHeight());
g.setColor(Color.ORANGE);
for (int i = 0; i < wall.length; i++) {
for (int j = 0; j < wall[i].length; j++) {
if (wall[i][j] == 1) {
g.fillRect(j * CELL_WIDTH i * CELL_WIDTH CELL_WIDTH
CELL_WIDTH);
}
}
}
g.setColor(Color.RED);
g.fillRect(CELL_WIDTH * currentPoint.x CELL_WIDTH * currentPoint.y
CELL_WIDTH CELL_WIDTH);
}
/**
* 当前节点
*/
public Point currentPoint = new Point(0 0);
/**
* 计时器
*/
public void action() {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!S2.isEmpty()) {
currentPoint = S2.pop();
repaint();
}
}
} 1000 200);
}
private int[][] wall = {
{ 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 }
{ 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 1 }
{ 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 1 1 1 1 1 1 }
{ 1 1 0 1 1 0 0 0 0 1 0 1 0 1 1 0 1 0 1 0 1 1
1 0 1 1 1 1 1 1 }
{ 1 1 0 0 0 1 1 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0
0 0 0 0 0 0 0 1 }
{ 1 1 0 1 0 0 0 0 0 1 0 1 0 1 1 0 1 1 0 0 1 0
1 1 1 0 1 1 0 1 }
{ 1 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 1 0 0 1 0
0 0 0 1 0 0 0 1 }
{ 1 0 0 1 1 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 1 0
1 1 1 1 0 1 1 1 }
{ 1 1 1 1 1 0 0 0 0 0 0 1 0 1 1 0 1 1 1 0 0 1
0 0 0 0 0 1 1 1 }
{ 1 0 0 0 0 0 1 1 0 1 0 1 0 1 0 0 1 0 0 1 0 1
0 1 1 1 1 1 0 1 }
{ 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 1
0 0 0 1 0 0 0 1 }
{ 1 0 0 1 1 0 1 1 0 0 0 0 0 0 1 0
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-03-30 18:40 Zjz\
文件 301 2013-07-18 23:05 Zjz\.classpath
文件 379 2013-07-18 23:05 Zjz\.project
目录 0 2014-03-30 18:40 Zjz\.settings\
文件 629 2013-07-18 23:05 Zjz\.settings\org.eclipse.jdt.core.prefs
目录 0 2014-03-30 18:40 Zjz\bin\
目录 0 2014-03-30 18:40 Zjz\bin\com\
目录 0 2014-03-30 18:40 Zjz\bin\com\zjz\
目录 0 2014-03-30 18:40 Zjz\bin\com\zjz\Queue\
文件 799 2014-01-21 22:51 Zjz\bin\com\zjz\Queue\BFS$1.class
文件 7957 2014-01-21 22:51 Zjz\bin\com\zjz\Queue\BFS.class
文件 1026 2014-01-21 22:51 Zjz\bin\com\zjz\Queue\Point.class
目录 0 2014-03-30 18:40 Zjz\src\
目录 0 2014-03-30 18:40 Zjz\src\com\
目录 0 2014-03-30 18:40 Zjz\src\com\zjz\
目录 0 2014-03-30 18:40 Zjz\src\com\zjz\Queue\
文件 7698 2013-08-01 23:18 Zjz\src\com\zjz\Queue\BFS.java
文件 138 2013-07-20 18:39 Zjz\src\com\zjz\Queue\BFS.java.bak
- 上一篇:java神州数码面试题
- 下一篇:Spring_控制反转_工厂模式
评论
共有 条评论