簡體   English   中英

迷宮圖像二維陣列的問題

[英]Problems with Maze image 2d Array

我必須編寫一個程序來解決迷宮圖像,然后決定將圖像傳遞給更易於閱讀的內容,因此我將圖像轉換為2D數組,如下所示:
#:黑牆
':空格
R:開始(我知道在哪里被閱讀)
B:結束(我知道哪里是藍色)

問題是我用字符表示每個像素,所以我有一個441 x 441 2d數組。 我的問題是:如何在不損失迷宮比例的情況下簡化2d數組中的元素數量?

我有這個:

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

我想要這個

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

我只想刪除空格,這樣就不必檢查每個空格,問題是我必須確定必須為每一列和每一行刪除多少空格。

經過大量的工作,我能夠使用A *算法解決問題,這是我的情況的解決方案,但是有很多算法可用於解決迷宮圖像:

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

我不確定您到底要什么,但是如果您要減小代表迷宮的數組的大小,則可以使用鋸齒狀的數組。 至少對於其中一種尺寸。 參見http://msdn.microsoft.com/zh-cn/library/2s05feca(v=vs.110).aspx

然后,您可以用一個值和一個計數替換幾個重復的值。

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

Jagged:
 # 7
 # ' 9
 # ' 9
 # ' 9

這是我實現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一半左右一路下滑。 這樣就可以像訪問GridHex結構數組一樣訪問MaxeMap的實例。

ICoordsUser和ICoordsCanon接口分別支持矩形或傾斜(即120度軸)上的十六進制網格操作,並自動轉換為另一種。 對於在Point實例中通過就足夠的正交網格,這是不必要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM