7717浏览
查看: 7717|回复: 2

[讨论] Processing系列 迷宫

[复制链接]

高级难度
深度优先算法


代码如下:
  1. void setup() {
  2.   size(300, 300);
  3.   background(55);
  4.   noStroke();
  5.   fill(255);
  6.   int n=12, n2=n*2;
  7.   float w=width/float(n2+1);
  8.   int[][] map=new int[n2][n2];
  9.   int x=0, y=0;
  10.   ArrayList<Point> points = new ArrayList<Point>();
  11.   points.add(new Point(0, 0));
  12.   ArrayList<ComplexPoint> complexPoints = new ArrayList<ComplexPoint>();
  13.   for (int j=0; j<n2; j++) {
  14.     for (int i=0; i<n2; i++) {
  15.       if (i%2==0 && j%2==0) {
  16.         map[j][i]=1;
  17.       }
  18.     }
  19.   }
  20.   while (!points.isEmpty ()) {
  21.     int count=0;
  22.     if (x-2>0 && map[y][x-2]==1) {
  23.       complexPoints.add(new ComplexPoint(x-2, y, x-1, y));
  24.       count++;
  25.     }
  26.     if (x+2<n2 && map[y][x+2]==1) {
  27.       complexPoints.add(new ComplexPoint(x+2, y, x+1, y));
  28.       count++;
  29.     }
  30.     if (y-2>0 && map[y-2][x]==1) {
  31.       complexPoints.add(new ComplexPoint(x, y-2, x, y-1));
  32.       count++;
  33.     }
  34.     if (y+2<n2 && map[y+2][x]==1) {
  35.       complexPoints.add(new ComplexPoint(x, y+2, x, y+1));
  36.       count++;
  37.     }
  38.     if (count>0) {
  39.       points.add(new Point(x, y));
  40.       ComplexPoint complexPoint = complexPoints.get(int(random(count)));
  41.       map[complexPoint.y][complexPoint.x] = 2;
  42.       map[complexPoint.py][complexPoint.px] = 2;
  43.       x = complexPoint.x;
  44.       y = complexPoint.y;
  45.       complexPoints.clear();
  46.     } else {
  47.       Point point = points.get(points.size()-1);
  48.       x = point.x;
  49.       y = point.y;
  50.       points.remove(points.size()-1);
  51.     }
  52.   }
  53.   rect(0, 0, width, w);
  54.   rect(0, 0, w, height);
  55.   for (int j=0; j<n2; j++) {
  56.     for (int i=0; i<n2; i++) {
  57.       if (map[j][i]==0) {
  58.         rect(w*(i+1), w*(j+1), w, w);
  59.       }
  60.     }
  61.   }
  62. }
  63. class Point {
  64.   int x, y;
  65.   Point(int x, int y) {
  66.     this.x=x;
  67.     this.y=y;
  68.   }
  69. }
  70. class ComplexPoint {
  71.   int x, y, px, py;
  72.   ComplexPoint(int x, int y, int px, int py) {
  73.     this.x=x;
  74.     this.y=y;
  75.     this.px=px;
  76.     this.py=py;
  77.   }
  78. }
复制代码




授权转载自 任远媒体实验室

大连林海  初级技神

发表于 2015-8-30 12:36:21

没有接触过 ,不过 这个一定很酷啊:P
回复

使用道具 举报

iooops  中级技匠

发表于 2015-12-10 23:43:09

没有小人走啊  = =
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail