我想做廣度優先搜索算法作爲迷宮求解器。迷宮是由一個TableLayoutPanel構成的。如何以不同的方式命名Hashtable中的按鍵
我有這樣的事情:
Hashtable hash = new Hashtable();
Queue<TableLayoutPanelCellPosition> q_cellposition = new Queue<TableLayoutPanelCellPosition>();
while (q_cellposition.Count != 0)
{
TableLayoutPanelCellPosition currentPosition = q_cellposition.Dequeue();
TableLayoutPanelCellPosition left = new TableLayoutPanelCellPosition(currentPosition.Column - 1, currentPosition.Row);
Label leftN = (Label)tlp.GetControlFromPosition(left.Column, left.Row);
TableLayoutPanelCellPosition right = new TableLayoutPanelCellPosition(currentPosition.Column + 1, currentPosition.Row);
Label rightN = (Label)tlp.GetControlFromPosition(right.Column, right.Row);
hash.Add(leftN, currentPosition);
q_cellposition.Enqueue(left);
hash.Add(rightN, currentPosition);
q_cellposition.Enqueue(right);
}
現在,在第一次迭代之後,它顯然試圖將新KeyValuePair添加到哈希表。然而,它將新的leftN鍵和rightN鍵看作相同的標籤(儘管它們在技術上不是這樣),並且它引發了錯誤,即該鍵已被添加。我想我每次向Hashtable添加內容時都必須更改密鑰的名稱。我現在的問題是:我如何有效地做到這一點?