迷宫代码:
截图如下:
package com.zxl.maze;/*
* 抽象类表示选择不同的算法*/
public abstract class AbstractMap
{
/*
* 得到数据*/
public abstract boolean[][] getData(int m,int n);
/*
* 重置*/
public abstract void reset(int m,int n);
}package com.zxl.maze;
/*
*深度优先,生成迷宫*/
import java.awt.Point;
import java.util.Random;
import java.util.Stack;public class DFSMap extends AbstractMap
{
private int m = 30, n = 30;
private boolean a[][];
private int currX, currY;
private Stack<Point> stack;// 保存已走路径
private int[] X_GO1 = { -2, 2, 0, 0 };
private int[] Y_GO1 = { 0, 0, 2, -2 };
private int[] X_GO2 = { -1, 1, 0, 0 };
private int[] Y_GO2 = { 0, 0, 1, -1 };public DFSMap()
{
stack = new Stack<Point>();
}@Override
public boolean[][] getData(int m,int n)// 得到数据
{
// TODO Auto-generated method stubreset(m, n);
return a;
}@Override
public void reset(int m, int n)// 重置
{
// TODO Auto-generated method stuba = new boolean[2 * m + 1][2 * n + 1];
this.m = 2 * m + 1;
this.n = 2 * n + 1;
for (int i = 0; i < this.m; i++)
for (int j = 0; j < this.n; j++)
a[i][j] = false;
currX = 1;
currY = 1;
stack.clear();
stack.push(new Point(1, 1));
a[1][0] = true;
a[1][1] = true;
start();
}private boolean check()// 判断是否全部走过。。。
{
for (int i = 1; i < m; i += 2)
{
for (int j = 1; j < n; j += 2)
{
if (a[i][j] == false)
return false;
}
}
return true;
}private void forward()// 后退一步
{
if (stack.size() > 0)
{
Point p = stack.pop();
currX = (int) p.getX();
currY = (int) p.getY();
}
else
{
goRandPosition();
}
}private void goRandPosition()
{
int i, j;
while (true)
{
i = (int) (Math.random() * m);
j = (int) (Math.random() * n);
if (a[i][j] == true && i % 2 == 1 && j % 2 == 1)
{
stack.push(new Point(i, j));
currX = i;
currY = j;
break;
}
}
}private void start()// 具体操作
{
while (!check())
{
go();
}
a[m - 2][n - 1] = true;
}private void go()
{
int orders[] = getOrder(4);
for (int i = 0; i < orders.length; i++)
{
if (isSafe(currX + X_GO1[orders[i]], currY + Y_GO1[orders[i]]))// 上
{
goNext(orders[i]);
return;
}
}
forward();
}private void goNext(int i)// 下一步
{
// TODO Auto-generated method stub
a[currX + X_GO1[i]][currY + Y_GO1[i]] = true;
a[currX + X_GO2[i]][currY + Y_GO2[i]] = true;
stack.push(new Point(currX, currY));
currX += X_GO1[i];
currY += Y_GO1[i];
}private int[] getOrder(int i)// 产生随机序列
{
// TODO Auto-generated method stub
int a[] = new int[i];
Random ran = new Random();
for (int j = 0; j < i; j++)
a[j] = j;
for (int k = 0; k < i; k++)
{
int r1 = ran.nextInt(i);
int r2 = ran.nextInt(i);
int b = a[r1];
a[r1] = a[r2];
a[r2] = b;
}
return a;
}private boolean isSafe(int x, int y)
{if (x < 0 || x >= m || y < 0 || y >= n || a[x][y] == true)
{
return false;
}
return true;
}}
package com.zxl.maze;
/*
* 随机prim算法的核心是(翻译的维基):
1、让迷宫全是墙
2、选一个格作为迷宫的通路,然后把它的邻墙放入列表
3、当列表里还有墙时
——1、从列表里随机选一个墙,如果它对面的格子不是迷宫的通路
————1、把墙打通,让对面的格子成为迷宫的通路
————2、把那个格子的邻墙加入列表
——2、如果对面的格子已经是通路了,那就从列表里移除这面墙*/
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class PriMap extends AbstractMap
{
private int m = 0, n = 0;
private boolean a[][];
private int currX, currY;// 记录当前格子位置
private List<Point> set;// 用作队列,存放墙
private int[] X_GO1 = { 1, 0, -1, 0 };
private int[] Y_GO1 = { 0, -1, 0, 1 };public void reset(int m, int n)
{
this.m = 2 * m + 1;
this.n = 2 * n + 1;
a = new boolean[this.m][this.n];
for (int i = 0; i < this.m; i++)
for (int j = 0; j < this.n; j++)
a[i][j] = false;
currX = 1;
currY = 1;
set.clear();
set.add(new Point(1, 2));
set.add(new Point(2, 1));
a[1][0] = true;
a[1][1] = true;
go();
a[this.m - 2][this.n - 1] = true;}
public void go()
{
while (set.size() > 0)
{
if (choiceWall())// 选择墙
{
addWall(currX, currY);// 将墙的临墙加入队列
}
}}
private boolean choiceWall()
{
// TODO Auto-generated method stub
Random r = new Random(System.currentTimeMillis());
Point p = set.remove(r.nextInt(set.size()));
int x = (int) p.getX();
int y = (int) p.getY();
if (test(x, y) == 1)
{
if (isSafe(x - 1, y) && a[x - 1][y] == false)
{
a[x][y] = true;
currX = x - 1;
currY = y;
a[currX][currY] = true;
return true;
}
else if (isSafe(x + 1, y) && a[x + 1][y] == false)
{
a[x][y] = true;
currX = x + 1;
currY = y;
a[currX][currY] = true;
return true;
}
}
else if (test(x, y) == 2)
{
if (isSafe(x, y - 1) && a[x][y - 1] == false)
{
a[x][y] = true;
currX = x;
currY = y - 1;
a[currX][currY] = true;
return true;
}
else if (isSafe(x, y + 1) && a[x][y + 1] == false)
{
a[x][y] = true;
currX = x;
currY = y + 1;
a[currX][currY] = true;
return true;
}
}
return false;
}private void addWall(int x, int y)
{
// TODO Auto-generated method stub
for (int i = 0; i < X_GO1.length; i++)
{
if (isSafe(currX + X_GO1[i], currY + Y_GO1[i]))
{
if (!set.contains(new Point(currX + X_GO1[i], currY + Y_GO1[i])))
{
set.add(new Point(currX + X_GO1[i], currY + Y_GO1[i]));
}
}
}
}private int test(int x, int y)
{
if (x % 2 == 0)
return 1;// 竖线
else if (y % 2 == 0)
return 2;// 横线
else
return 0;// 出错了
}private boolean isSafe(int x, int y)
{if (x <= 0 || x >= m || y <= 0 || y >= n || a[x][y] == true)
{
return false;
}
return true;
}@Override
public boolean[][] getData(int m,int n)
{
// TODO Auto-generated method stub
set = new ArrayList<Point>();
reset(m, n);
return a;
}
}package com.zxl.maze;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;import javax.swing.JPanel;
public class MainPanel extends JPanel
{
int x = 0;
int y = 0;
boolean b[][]=null;public MainPanel()
{
this.setSize(400, 400);}
public void setMap(boolean b[][])
{
this.b = b;
}@Override
public Dimension getPreferredSize()
{
// TODO Auto-generated method stub
return new Dimension(400,400);
}public void paintMap(int x, int y)
{
this.x = x;
this.y = y;repaint();
}@Override
public void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
if (x == 0&&y==0)
{
g.clearRect(0, 0, this.getWidth(), this.getHeight());
}
else
{
for(int i=0;i<=x;i++)
for(int j=0;j<=y;j++)
{
if (b[i][j] == true)
{
g.setColor(Color.blue);
g.fillRect(50 + i * 10, 50 + j* 10, 10, 10);
}
else
{
g.setColor(Color.red);
g.fillRect(50 + i * 10, 50 + j * 10, 10, 10);
}}
}
}}
package com.zxl.maze;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;public class MainFrame extends JFrame
{
//JButton resetButton;
Container con;
boolean b[][];
JMenu menu;
JMenuBar bar;
JMenuItem item;
MainPanel pan=new MainPanel();
public MainFrame()
{
this.setSize(600,550);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//resetButton=new JButton("重 绘");
con=this.getContentPane();
con.setLayout(new BorderLayout());
//con.add(resetButton,BorderLayout.SOUTH);
menu=new JMenu("菜单");
item=new JMenuItem("绘制地图");
bar=new JMenuBar();
bar.add(menu);
menu.add(item);
this.add(pan);
this.setJMenuBar(bar);
this.setVisible(true);
//setMap();
item.addActionListener(new ActionListener()
{@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
setMap();
Thread t=new Thread(new DrawHandler(pan));
t.start();
}
});
}
private void setMap()
{
// TODO Auto-generated method stub
this.requestFocus();
AbstractMap map=new DFSMap();
b=map.getData(20, 20);
pan.setMap(b);
/*for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[0].length;j++)
{
pan.paintMap(i, j);
try
{
Thread.sleep(10);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}*/
}class DrawHandler implements Runnable{
private MainPanel pan;
public DrawHandler(MainPanel pan){
this.pan=pan;
}
public void run(){
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[0].length;j++)
{
pan.paintMap(i, j);
try
{
Thread.sleep(10);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
}
}package com.zxl.maze;
public class Test
{
public static void main(String []args)
{
new MainFrame();
}
}package com.zxl.maze;
import java.util.Random;
/*
*深度优先,生成迷宫*/public class TreeMap extends AbstractMap
{
private int m = 0, n = 0;
private boolean a[][];
private int currX, currY;
private int[] X_GO1 = { -2, 2, 0, 0 };
private int[] Y_GO1 = { 0, 0, 2, -2 };
private int[] X_GO2 = { -1, 1, 0, 0 };
private int[] Y_GO2 = { 0, 0, 1, -1 };@Override
public boolean[][] getData(int m,int n)// 得到数据
{
// TODO Auto-generated method stub
reset(m,n);
return a;
}@Override
public void reset(int m, int n)// 重置
{
// TODO Auto-generated method stub
a = new boolean[2 * m + 1][2 * n + 1];
this.m = 2 * m + 1;
this.n = 2 * n + 1;
for (int i = 0; i < this.m; i++)
for (int j = 0; j < this.n; j++)
a[i][j] = false;
currX = 1;
currY = 1;
a[1][0] = true;
a[1][1] = true;
start();
}private boolean check()// 判断是否全部走过。。。
{
for (int i = 1; i < m; i += 2)
{
for (int j = 1; j < n; j += 2)
{
if (a[i][j] == false)
return false;
}
}
return true;
}private void forward()// 后退一步
{
int i, j;
while (true)
{
i = (int) (Math.random() * m);
j = (int) (Math.random() * n);
if (a[i][j] == true && i % 2 == 1 && j % 2 == 1)
{
currX = i;
currY = j;
return;
}
}}
private void start()// 具体操作
{
while (!check())
{
go();
}
a[m - 2][n - 1] = true;
}private void go()
{
int orders[] = getOrder(4);
for (int i = 0; i < orders.length; i++)
{
if (isSafe(currX + X_GO1[orders[i]], currY + Y_GO1[orders[i]]))// 上
{
goNext(orders[i]);
return;
}
}
forward();
}private void goNext(int i)// 下一步
{
// TODO Auto-generated method stub
a[currX + X_GO1[i]][currY + Y_GO1[i]] = true;
a[currX + X_GO2[i]][currY + Y_GO2[i]] = true;
currX += X_GO1[i];
currY += Y_GO1[i];
}private int[] getOrder(int i)// 产生随机序列
{
// TODO Auto-generated method stub
int a[] = new int[i];
Random ran = new Random();
for (int j = 0; j < i; j++)
a[j] = j;
for (int k = 0; k < i; k++)
{
int r1 = ran.nextInt(i);
int r2 = ran.nextInt(i);
int b = a[r1];
a[r1] = a[r2];
a[r2] = b;
}
return a;
}private boolean isSafe(int x, int y)
{if (x < 0 || x >= m || y < 0 || y >= n || a[x][y] == true)
{
return false;
}
return true;
}}
java写的迷宫代码
时间: 2024-10-07 05:16:23
java写的迷宫代码的相关文章
一个比较好的Java写的计算器代码 (转)
1 import java.awt.BorderLayout; 2 import java.awt.Color; 3 import java.awt.GridLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 import javax.swing.JPan
Java实现可视化迷宫
代码地址如下:http://www.demodashi.com/demo/14547.html 需求 使用深度优先算法求解迷宫路径,使用Java实现求解过程的可视化,可单步运行,形象直观. 演示效果 红色格子为迷宫终点,迷宫可放大缩小,为了录屏选择了较小的尺寸,有多种不同难度的迷宫可以加载. 简单迷宫 复杂迷宫 项目运行 文件中有两个运行脚本,Windows下直接双击win运行.bat即可,linux和Mac运行sh文件中的命令即可,喜欢用IDE的也可自行创建项目. 运行项目后,点击菜单栏左上角
用java写一个远程视频监控系统,实时监控(类似直播)我想用RPT协议,不知道怎么把RPT协议集成到项目中
我最近在用java写一个远程视频监控系统,实时监控(类似直播)我想用RPT协议,不知道怎么把RPT协议集成到项目中,第一次写项目,写过这类项目的多多提意见,哪方面的意见都行,有代码或者demo的求赏给我,谢谢
使用Java写定时器
Java编写处理定时任务主要用到的类是java.util.Timer和java.util.TimerTask: 如下几行代码就可以实现一个具有定时执行任务的定时器: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import MailSender.MailSe
kafka集群搭建和使用Java写kafka生产者消费者
kafka集群搭建 Java代码 1.zookeeper集群 搭建在110, 111,112 2.kafka使用3个节点110, 111,112 修改配置文件config/server.properties broker.id=110 host.name=192.168.1.110 log.dirs=/usr/local/kafka_2.10-0.8.2.0/logs 复制到其他两个节点,然后修改对应节点上的config/server.pro 3.启动,在三个节点分别执行 bin/kaf
用JAVA写一个视频播放器
前言 跳过废话,直接看正文 当年入坑java是因为它的跨平台优势.那时我认为,"编写一次,处处运行."这听上去多么牛逼,应该是所有语言发展的终极之道,java势必会一统天下. 然而事实证明,那时的我还是太年轻. 正所谓鱼和熊掌不可兼得,若要享受跨平台带来的方便,便不可避免地要接受性能上的不足.事实上,java一直在致力于提高虚拟机的性能(JIT等技术),但面对对实时计算性能要求很高或涉及到用硬件优化的任务(视频的硬件编码.解码)时,仍远远比不上c或c++.因此,很少能够看到有人用jav
写了个代码统计工具
周末想统计下XCL-Charts到底写了多少行代码,就用Go语言写了个代码统计工具,效果还不错,统计速度也快. 就把统计报表部份今天完善了下,硬生生把很短的代码澎涨到200多行了. 除了必要的代码行数和文件总计外,增加了代码行数有300,500,1000,5000及5000以上这几个级别的代码文件个数统计功能. 同时,用户可以指定显示出代码行数大于等于某值的所有文件明细,并按从大到小顺序排列. 贴下效果: E:\XCLTools\src\crname>go run scounter.go -l=
C#和JAVA中编写事务代码
C# DAL层代码,执行多条增删改,使用事务操作: /// <summary> /// 执行 多条增删改 (非查询语句) /// </summary> /// <param name="strSql"></param> /// <param name="paras"></param> /// <returns></returns> public static int E
关于java加壳和代码混淆
关于java加壳和代码混淆 在C中,进行代码加密,首要经过加壳的方法.所谓加壳,即是先将程序代码加密,然后用特定的程序加载器,将代码解密后加载进内存,这样能够在避免代码的反编译,当然,有加壳东西,也有解壳东西,尽管不能100%避免crack,但仍然给代码增加一层有力的维护. 然而在Java中,维护代码是件很困难的工作,由于class文件十分标准,很容易反编译,且反编译后的代码明晰可读.常见的维护办法是运用代码混淆器,打乱class和function以及变量的姓名,能够搅扰反编译后的代码的可读性.