private static int[,] mazeMap = new int[ + , + ];//0<=x<=12 0<=y<=24
private static Random Rd = new Random();
首先声明mazeMap存储数据,声明了一个15*27的迷宫,其中最外面的一圈是用来做墙的。
public void GenerateMap() { //初始化
for (int i = ; i <= + ; i++) {
for (int j = ; j <= + ; j++) {
if (i == || i == + || j == || j == + ) //初始化迷宫
mazeMap[i, j] = ;
else
mazeMap[i, j] = ;
}
}
这一块是初始化墙和内部的构造。其中我们约定,0是路,1是墙。
接下来贴出之后的算法。
public void GenerateMap() {
for (int i = ; i <= + ; i++) {
for (int j = ; j <= + ; j++) {
if (i == || i == + || j == || j == + ) //初始化迷宫
mazeMap[i, j] = ;
else
mazeMap[i, j] = ;
}
} Create( * (Rd.Next() % ( / ) + ), * (Rd.Next() % ( / ) + )); //从随机一个点开始生成迷宫,该点行列都为偶数
for (int i = ; i <= + ; i++) //边界处理
{
mazeMap[i, ] = ;
mazeMap[i, + ] = ;
} for (int j = ; j <= + ; j++) //边界处理
{
mazeMap[, j] = ;
mazeMap[ + , j] = ;
}
mazeMap[, ] = ; //给定入口
mazeMap[ - , ] = ; //给定出口
}
public void Create(int x, int y) //随机生成迷宫
{
int[,] c = new int[, ] { { , }, { , }, { , - }, { -, } }; //四个方向
for (int i = ; i < ; i++)
{
int j = Rd.Next() % ; int t = c[i, ];
c[i, ] = c[j, ];
c[j, ] = t;
t = c[i, ];
c[i, ] = c[j, ];
c[j, ] = t;
}
mazeMap[x, y] = ;
for (int i = ; i < ; i++)
if (mazeMap[x + * c[i, ], y + * c[i, ]] == ) {
mazeMap[x + c[i, ], y + c[i, ]] = ; Create(x + * c[i, ], y + * c[i, ]);
}
}
审视这段代码,如果我们使用一些技巧,代码可读性将会更好,其中的13都是Height,迷宫高度;25都是Width,迷宫宽度,当读到13和25时,可以自动代入Height与Width去理解。
算法的原理很简单,就是递归生成迷宫。
Create(x + * c[i, ], y + * c[i, ]);
这一行就是关键递归代码。
参考文献:http://wenku.baidu.com/view/f22455126edb6f1aff001f13.html
虽说是百度文库的,但是值得一读。
下面贴出完整代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Security.Cryptography.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace Maze
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private static int[,] mazeMap = new int[ + , + ];//0<=x<=12 0<=y<=24
private static Random Rd = new Random();
public static List<Rect> Rects { get; set; } public MainPage() {
this.InitializeComponent(); Rects = new List<Rect>(); GenerateMap();
for (int x = ; x < ; x++)//25*14
{
var rect = new Rect() { X = x / , Y = x % };
switch (mazeMap[rect.X + , rect.Y + ]) {
case :
rect.Color = "Blue";
break;
case :
rect.Color = "Gray";
break;
case :
rect.Color = "Red";
break;
case :
rect.Color = "Yellow";
break; }
Rects.Add(rect);
}
GridView.ItemsSource = Rects; } public class Rect
{
public int X { get; set; }
public int Y { get; set; }//以左上角为1,1
public string Color { get; set; }
} public void GenerateMap() {
for (int i = ; i <= + ; i++) {
for (int j = ; j <= + ; j++) {
if (i == || i == + || j == || j == + ) //初始化迷宫
mazeMap[i, j] = ;
else
mazeMap[i, j] = ;
}
} Create( * (Rd.Next() % ( / ) + ), * (Rd.Next() % ( / ) + )); //从随机一个点开始生成迷宫,该点行列都为偶数
for (int i = ; i <= + ; i++) //边界处理
{
mazeMap[i, ] = ;
mazeMap[i, + ] = ;
} for (int j = ; j <= + ; j++) //边界处理
{
mazeMap[, j] = ;
mazeMap[ + , j] = ;
}
mazeMap[, ] = ; //给定入口
mazeMap[ - , ] = ; //给定出口
} public void Create(int x, int y) //随机生成迷宫
{
int[,] c = new int[, ] { { , }, { , }, { , - }, { -, } }; //四个方向
for (int i = ; i < ; i++)
{
int j = Rd.Next() % ; int t = c[i, ];
c[i, ] = c[j, ];
c[j, ] = t;
t = c[i, ];
c[i, ] = c[j, ];
c[j, ] = t;
}
mazeMap[x, y] = ;
for (int i = ; i < ; i++)
if (mazeMap[x + * c[i, ], y + * c[i, ]] == ) {
mazeMap[x + c[i, ], y + c[i, ]] = ; Create(x + * c[i, ], y + * c[i, ]);
}
}
}