#!perl
# labir.pl -- maze generator
# Copyright (C) Zsban Ambrus 2001--2009
use warnings; use strict; use 5.010;
my $W = 35;
my $H = 19;
my @a = ([(1)x(3+2*$W)], map({ [1, (0)x(1+2*$W), 1] } 1 .. 1+2*$H), [(
+1)x(3+2*$W)]);
for my $u (0 .. $H-1) {
for my $v (0 .. $W-1) {
$a[2+2*$u][2+2*$v] = 1;
}
}
my @y = (-1)x($W*$H);
my $C; $C = sub {
no warnings "recursion";
my($v) = @_;
my $w = $y[$v];
if ($w < 0) {
return $v;
} else {
my $z = &$C($w);
$y[$v] = $z;
return $z;
}
};
my $U = sub {
my($u, $v) = @_;
my($c, $d) = (&$C($u), &$C($v));
$c != $d and
$y[$c] = $d;
};
while (&$C(0) != &$C($W*$H-1)) {
my $a = 1 < rand(2);
my $x = int(rand($H));
my $y = int(rand($W));
$a ? $y < $W - 1 : $x < $H - 1 or next;
my ($u, $v) = (($x+!$a)*$W+($y+$a), $x*$W+$y);
&$C($u) != &$C($v) || rand(5) < 1 or next; # here's the magic
&$U($u, $v);
$a[2+!$a+2*$x][2+$a+2*$y] = 1;
}
for my $r (1 .. 1+2*$H) {
for my $c (1 .. 1+2*$W) {
my $o = $a[$r][$c];
my $v = $a[$r-1][$c] && $a[$r+1][$c];
my $h = $a[$r][$c-1] && $a[$r][$c+1];
print(
2 == $r && 2 == $c ? "@" :
2*$H == $r && 1+2*$W == $c ? ">" :
$o ? " " :
$v ? ($h ? " " : "-") :
($h ? "|" : "+")
);
}
print "\n";
}
__END__
Here's an example output.
+-------+---+---+-----+---+-----+-+-+-----+-------+-+-+---+---------+-
++
|@ | | | | | | | | | | | | | |
+|
+-- | +-+---+-- | ----+ | | --- | | +-+ +-+-+-+---+ +-+ | | | +---+-+
+|
| | | | | | | | | | | | | | | | | | | |
+|
+-+ +-+-+ | --+-+-+ +-+ | +-+ +-+-+ | | +-+ | --+ +-- +-+ +-+-+ | --
++
| | | | | | | | | | | | | | | | | | |
+|
+-+ | --+-+-+-+-+ | --+ ----+-+-+-+ +-+ +-- +-+---+ +-- +-- --+-+-- |
+|
| | | | | | | | | | | | | | | | |
+|
+---+ | +-+-+-----+ | | +-+-+-+-+-+ | +-+ +-- | | +-+-+-- +-+ | --+-
++
| | | | | | | | | | | | | | | | | | | |
+|
| --+ +---+ +-+ +-+ --+ +-+-+-+ +-+ +-+-+-+ | --+-+ | | +---+ | | +-+
+|
| | | | | | | | | | | | | | | | | | |
+|
+-+-- +-+ | +-+ +-+-- +-+-+-+ +-+ | +-+-- +-+-+-+-+-+-+ | +-+ +-+ +-
++
| | | | | | | | | | | | | | | | | | | | | |
+|
| +-- +-+ +-+-+-- +-+ | +-+ +-- +-- --+ +---+ | +-+-+ | | +-+ | --+ |
+|
| | | | | | | | | | | | | | |
+|
| +---+-+-+-- +-+ --+ | | +-+-- | | | +-+ | +-+ | | +-+-+ +-+ ----+ +-
++
| | | | | | | | | | | | | | | | | | | | | |
+|
+-+-+ +-+-+-+ | +-- | +-+ | | --+-+-+-+-+ +-+ +-+-+ | +-+ --+ | | | +-
++
| | | | | | | | | | | | | | | | | | | | |
+|
+-+ +-+-- +-+-+ +-+-+---+ +-+-- | ----+-+ | +-- +-+ | | --+ | +---+
+|
| | | | | | | | | | | | | |
+|
+-+ +-+---+ +-- | +-+-+ | | +-------- +-+ | +-+ +-+ +-+ | +-+---- ----
++
| | | | | | | | | | | | | | | |
+|
| | --+ | --+ | | --+ | +-- +---+ --+ +-- | --+-----+-+-+ | | | | | +-
++
| | | | | | | | | | | | | | | | | | |
+|
+-+ | +-+-----+---- +-+-+ | --+---+-+-+ | +---+---+ +-+ +-+-+-+ | |
+|
| | | | | | | | | | | | | | | | | | | | | |
+|
| | +-+-+ +-- +-- --+-+ | | +---+ --+ +-+ | | +-+-+-+ | | | | | +-+ |
+|
| | | | | | | | | | | | | | | | | | | | | | |
+|
+-+ +-+-+ +---------+ | +-+-+ --+ --+ | +-+-+-+ | +-+-+ | | +-+ | | |
+|
| | | | | | | | | | | | | | | | | |
+|
| | | | | | +-----+-+-+ | | | ----+ +-+-+-+ | +-+-+ | | +-+ +-- | | |
+|
| | | | | | | | | | | | | | | | | | | | |
+|
| +---+-+-+ | | | +---- +-+ ----+-+ +-+-+-+ | +-- | +-+ +-+-- +-- |
+|
| | | | | | | | | | | | | |
+|
+-+ +-- --+---+-+ +-+-- +-+-- --+-+-- | | | | +-+ +-+-+-+ --+ +-+-+-+
+|
| | | | | | | | | | | | | | | | | | | | | | |
+>
+-+-------+-----+-+-+---+-+-----+-+---+---+-+---+-+-+-+-+---+-+-+-+-+-
++
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|