读书人

递归调用改成循环

发布时间: 2013-06-25 23:45:42 作者: rapoo

递归调用改为循环
我就是想把这个最上面的那个函数改成循环的,请问怎么改呢???在线等答案啊、、、、、

public void DisplayArround(Pane currentPane)
{
if (currentPane.State == PaneState.Opened || currentPane.HasMine)
{
return;
}
currentPane.Open();

List<Pane> panes = this.GetAroundPanes(currentPane);
foreach (Pane p in panes)
{
if (this.GetAroundMineCount(p) == 0)
{
DisplayArround(p);
}
else
{
if (p.State != PaneState.Opened && !currentPane.HasMine)
{
p.Open();
}
}
}
}


private List<Pane> GetAroundPanes(Pane pane)
{
List<Pane> result = new List<Pane>();
int paneWidth = pane.Width;
int paneHeight = pane.Height;
foreach (Pane p in this.Controls)
{
if (p.Top == pane.Top && Math.Abs(pane.Left - p.Left) == paneWidth
||
p.Left == pane.Left & Math.Abs(pane.Top - p.Top) == paneHeight
||


Math.Abs(p.Top - pane.Top) == paneHeight && Math.Abs(pane.Left - p.Left) == paneWidth
)
{
result.Add(p);
}
}
return result;

}


public int GetAroundMineCount(Pane pane)
{
int mineCount = 0;
List<Pane> panes=GetAroundPanes(pane);
foreach (Pane p in panes)
{
if (p.HasMine==true)
{
mineCount++;
}
}
return mineCount;
}
[解决办法]
改他干嘛,你这个需要上下级关系的操作,想改成循环,就遍历 panes ,这个必须是可以每个pane都可以访问到,然后对每个pane调用GetAroundPanes相关函数
[解决办法]
http://bbs.csdn.net/topics/390317532

建议楼主去看看
[解决办法]
递归改成非递归需要用到循环,并且自己维护递归需要的工作栈。具体做法请参看数据结构第二版,清华大学出版社。在讲到栈部分有例子,有一个固定的机械作法可以完成这个工作

读书人网 >.NET

热点推荐