此递归函数运行后提示陷入无穷递归,不知道问题在何处,求指导。
public int fk(int i, List<int>s)
{
int min = 999;
if (s.Count <=0)
return Convert.ToInt32(d[0, i]);
else
{
List<int> newt = new List<int>();
for (int k = 0; k < s.Count; k++)
{
s.ForEach(z => newt.Add(z));
int backup = newt[k];
newt.RemoveAt(k);
int value = fk(backup, newt);
if (min > value + Convert.ToInt32(d[backup, i]))
min = value + Convert.ToInt32(d[backup,i]);
}
return min;
}
}
------解决方案--------------------public int fk(int i, List<int>s)
{
int min = 999;
if (s.Count <=0)
return Convert.ToInt32(d[0, i]);
else
{
List<int> newt = new List<int>();
s.ForEach(z => newt.Add(z));
for (int k = 0; k < s.Count; k++)
{
int backup = newt[k];
newt.RemoveAt(k);
int value = fk(backup, newt);
if (min > value + Convert.ToInt32(d[backup, i]))
min = value + Convert.ToInt32(d[backup,i]);
}
return min;
}
}
------解决方案--------------------
我找出问题所在了,哈哈,刚才那个只保证跳出循环了,我知道你的意思假如原始为{1,2},先求{2},在求{2},最后得到值。
C# code
public int fk(int i, List<int> s)
{
int min = 999;
if (s.Count <= 0)
{
return Convert.ToInt32(d[0, i]);
}
else
{
for (int k = 0; k < s.Count; k++)
{
List<int> newt = new List<int>();
s.ForEach(z => newt.Add(z));
int backup = newt[k];
newt.RemoveAt(k);
int value = fk(backup, newt);
if (min > value + Convert.ToInt32(d[backup, i]))
min = value + Convert.ToInt32(d[backup, i]);
}
return min;
}
}