#!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.
+-------+---+---+-----+---+-----+-+-+-----+-------+-+-+---+---------+-
++
|@ | | | | | | | | | | | | | |
+|
+-- | +-+---+-- | ----+ | | --- | | +-+ +-+-+-+---+ +-+ | | | +---+-+
+|
| | | | | | | | | | | | | | | | | | | |
+|
+-+ +-+-+ | --+-+-+ +-+ | +-+ +-+-+ | | +-+ | --+ +-- +-+ +-+-+ | --
++
| | | | | | | | | | | | | | | | | | |
+|
+-+ | --+-+-+-+-+ | --+ ----+-+-+-+ +-+ +-- +-+---+ +-- +-- --+-+-- |
+|
| | | | | | | | | | | | | | | | |
+|
+---+ | +-+-+-----+ | | +-+-+-+-+-+ | +-+ +-- | | +-+-+-- +-+ | --+-
++
| | | | | | | | | | | | | | | | | | | |
+|
| --+ +---+ +-+ +-+ --+ +-+-+-+ +-+ +-+-+-+ | --+-+ | | +---+ | | +-+
+|
| | | | | | | | | | | | | | | | | | |
+|
+-+-- +-+ | +-+ +-+-- +-+-+-+ +-+ | +-+-- +-+-+-+-+-+-+ | +-+ +-+ +-
++
| | | | | | | | | | | | | | | | | | | | | |
+|
| +-- +-+ +-+-+-- +-+ | +-+ +-- +-- --+ +---+ | +-+-+ | | +-+ | --+ |
+|
| | | | | | | | | | | | | | |
+|
| +---+-+-+-- +-+ --+ | | +-+-- | | | +-+ | +-+ | | +-+-+ +-+ ----+ +-
++
| | | | | | | | | | | | | | | | | | | | | |
+|
+-+-+ +-+-+-+ | +-- | +-+ | | --+-+-+-+-+ +-+ +-+-+ | +-+ --+ | | | +-
++
| | | | | | | | | | | | | | | | | | | | |
+|
+-+ +-+-- +-+-+ +-+-+---+ +-+-- | ----+-+ | +-- +-+ | | --+ | +---+
+|
| | | | | | | | | | | | | |
+|
+-+ +-+---+ +-- | +-+-+ | | +-------- +-+ | +-+ +-+ +-+ | +-+---- ----
++
| | | | | | | | | | | | | | | |
+|
| | --+ | --+ | | --+ | +-- +---+ --+ +-- | --+-----+-+-+ | | | | | +-
++
| | | | | | | | | | | | | | | | | | |
+|
+-+ | +-+-----+---- +-+-+ | --+---+-+-+ | +---+---+ +-+ +-+-+-+ | |
+|
| | | | | | | | | | | | | | | | | | | | | |
+|
| | +-+-+ +-- +-- --+-+ | | +---+ --+ +-+ | | +-+-+-+ | | | | | +-+ |
+|
| | | | | | | | | | | | | | | | | | | | | | |
+|
+-+ +-+-+ +---------+ | +-+-+ --+ --+ | +-+-+-+ | +-+-+ | | +-+ | | |
+|
| | | | | | | | | | | | | | | | | |
+|
| | | | | | +-----+-+-+ | | | ----+ +-+-+-+ | +-+-+ | | +-+ +-- | | |
+|
| | | | | | | | | | | | | | | | | | | | |
+|
| +---+-+-+ | | | +---- +-+ ----+-+ +-+-+-+ | +-- | +-+ +-+-- +-- |
+|
| | | | | | | | | | | | | |
+|
+-+ +-- --+---+-+ +-+-- +-+-- --+-+-- | | | | +-+ +-+-+-+ --+ +-+-+-+
+|
| | | | | | | | | | | | | | | | | | | | | | |
+>
+-+-------+-----+-+-+---+-+-----+-+---+---+-+---+-+-+-+-+---+-+-+-+-+-
++
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|