问题与迷宫图像2D阵列

问题与迷宫图像2D阵列

问题描述:

我必须编写一个程序来解决迷宫图像,我决定将图像传递到更容易阅读的东西,所以我将图像转换成如下的二维数组:
#: blackwalls
':空格
R:开始(我知道在哪里和读)
B:端部(我知道在哪里和是蓝色)
问题与迷宫图像2D阵列

的问题是,I表示各像素在一个字符中,所以我有一个441 x 441二维数组。 这里我questino:我怎样才能简化我的二维数组中的元素数量,而不失迷宫比例?

我有这样的:

# # # # # # # 
# ' ' ' ' ' ' ' ' '  
# ' ' ' ' ' ' ' ' '  
# ' ' ' ' ' ' ' ' ' 

,我想这

# # # # # # # 
#  
# ' ' ' ' ' ' ' ' '  
# 

我只想要删除的白色空间,这样我就不必检查每个空间,问题是我必须确定每个列和每行必须删除多少空格(')。

+0

退房我如何阅读和在此建项目的迷宫地形:http://hexgridutilities.codeplex.com/我认为你需要的一切是在MazeMap.cs。 – 2013-04-29 01:46:56

+0

感谢您的回答,您能否向我解释一下他们在MazeMap.cs课程中做了些什么? – Moy 2013-04-29 04:14:35

+0

你能发布两个你需要解决的例子吗?最好的方法是(例如你需要编写多么复杂)取决于你需要什么样的迷宫 – Patashu 2013-04-29 04:50:54

工作的地块后,我能够通过使用A *算法的所有步骤都是在这个页面来解决问题:

http://www.policyalmanac.org/games/aStarTutorial.htm

这是我的情况的解决方案,但有很多的算法可用于解决迷宫图像:

http://en.wikipedia.org/wiki/Maze_solving_algorithm

我不确定你在问什么,但如果你想减小代表迷宫的数组的大小,你可以使用锯齿状的数组。至少对于其中一个维度。请参阅http://msdn.microsoft.com/en-us/library/2s05feca(v=vs.110).aspx

然后,您可以用一个值和一个计数替换多个重复值。

Original: 
# # # # # # # 
# ' ' ' ' ' ' ' ' ' 
# ' ' ' ' ' ' ' ' ' 
# ' ' ' ' ' ' ' ' ' 

Jagged: 
# 7 
# ' 9 
# ' 9 
# ' 9 
+0

那么您将如何解决迷宫? – Patashu 2013-04-29 04:52:14

+2

我知道起点在哪里,“'”符号是我可以去的地方,然后我可以开始检查每个位置(上,下,左和右),如果有#表示我不能去那个地方。 – Moy 2013-04-29 05:24:02

+0

@Patashu见Moises的评论。它只会改变你存储数据的方式,而不会改变你如何使用它。 – denver 2013-04-29 13:36:10

这里是我如何实现我的MazeMap的关键部分。它被设计用于六角形网格,所以连接与正交网格稍微偏离。

public sealed class MazeMap : MapDisplay { 
    protected override string[] Board { get { return _board; } } 
    string[] _board = new string[] { 
    ".............|.........|.......|.........|.............", 
     /* many similar lines omitted */ 
    ".............................|.......|.....|..........." 
    }; 

    public override bool IsPassable(ICoordsUser coords) { 
    return IsOnBoard(coords) && this[coords].Elevation == 0; 
    } 

    public override IMapGridHex this[ICoordsCanon coords] { 
    get {return this[coords.User];} 
    } 
    public override IMapGridHex this[ICoordsUser coords] { get { 
    return new GridHex(Board[coords.Y][coords.X], coords); 
    } } 

    public struct GridHex : IMapGridHex { 
    internal static MapDisplay MyBoard { get; set; } 

    public GridHex(char value, ICoordsUser coords) : this() { Value = value; Coords = coords; } 

    IBoard<IGridHex> IGridHex.Board   { get { return MyBoard; } } 
    public IBoard<IMapGridHex> Board   { get { return MyBoard; } } 
    public ICoordsUser   Coords   { get; private set; } 
    public int     Elevation  { get { return Value == '.' ? 0 : 1; } } 
    public int     ElevationASL { get { return Elevation * 10; } } 
    public int     HeightObserver { get { return ElevationASL + 1; } } 
    public int     HeightTarget { get { return ElevationASL + 1; } } 
    public int     HeightTerrain { get { return ElevationASL + (Value == '.' ? 0 : 10); } } 
    public char    Value   { get; private set; } 
    public IEnumerable<NeighbourHex> GetNeighbours() { 
     var @this = this; 
     return NeighbourHex.GetNeighbours(@this).Where(n=>@this.Board.IsOnBoard(n.Hex.Coords)); 
    } 
    } 
} 

注意大约一半向下的this的定义。这允许MaxeMap的一个实例像一个数组GridHex结构一样访问。

ICoordsUser和ICoordsCanon接口分别支持矩形或倾斜(即120度轴)的六角网格操作,并自动从一个转换到另一个;这在正交网格中是不必要的,在这个网格中足以传递一个Point实例。

+0

感谢您的帮助我将使用A *算法来解决迷宫问题 – Moy 2013-05-02 22:41:36