我有一個struct
其中包含了一些int
和bool
成員,我期待獲得列表中的最低值(實際進行的A *搜索基於路徑查找器) 。獲取列表的最低值<Struct.int>
基本上,我的目標是這樣的:
public struct Tile
{
public int id;
public int x;
public int y;
public int cost;
public bool walkable;
public int distanceLeft;
public int parentid;
}
而且我想用最低的distanceLeft的項目。名單宣佈像這樣:
List<Structs.Tile> openList = new List<Structs.Tile>();
和值以這種方式分配:
while (pathFound == null)
{
foreach (Structs.Tile tile in map)
{
foreach (Structs.Tile tile1 in getSurroundingTiles(Current))
{
if (tile1.x == tile.x && tile1.y == tile.y)
{
Structs.Tile curTile = tile1;
curTile.parentid = Current.id;
curTile.distanceLeft = (Math.Abs(tile.x - goalx) + Math.Abs(tile.y - goaly));
if (curTile.distanceLeft == 0)
{
pathFound = true;
}
openList.Add(curTile);
}
}
}
foreach (Structs.Tile tile in openList)
{
}
}
如果我猜我會說這要麼是非常困難或複雜得多,比我」讓它聽起來很不錯,或者非常容易,我只是感到困惑。
我的確在考慮滾動列表並將每個項目與其對應項目進行比較,但考慮到我們所處的年齡,這似乎不合理,似乎會有一種更簡單的方法。我不關心列表的順序,因爲我正在爲每個項目分配一個索引,我可以從中調用它。
在此先感謝!
你應該看看LINQ。 – antonijn 2013-02-19 21:19:06
爲什麼你使用一個結構?它消耗大約28個字節,比4/8字節的指針大得多。每個消耗你的結構的方法都需要攜帶28個字節。如果是班級,比較會更快。 – 2013-02-19 21:30:09
它可能是一個重複的mbeckish,但我不知道Linq是否足夠知道這是一個Linq函數,所以我對此表示誠摯的歉意。儘管如此,我還是希望保留這個線程,因爲它包含了我認爲可能對某些人(其他人可能不知道Linq或此處引用的協會)有用的響應。 – XtrmJosh 2013-02-19 21:34:11