gpt4 book ai didi

c++ - TSP递归解决方案的迭代形式

转载 作者:行者123 更新时间:2023-12-02 10:29:40 25 4
gpt4 key购买 nike

我有一个函数,可以返回n个城市之间的最短距离。又名旅行推销员问题。

int tsp(int mask, int pos, int n) 
{

if (mask == VISITED_ALL)
{
return dist[pos][0];
}

int result = 2147483647;

for (int i = 0; i < n; i++)
{

if ((mask & (1 << i)) == 0)
{

int new_result = dist[pos][i] + tsp(mask | (1 << i), i, n);
result = min(result, new_result);
}

}

return result;
}
我想将此递归解决方案修改为迭代形式,但是我很努力这样做。我正在遵循本指南,该指南描述了使用示例 https://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and从递归解决方案到迭代解决方案的转换
这是我尝试过的方法,但似乎没有用
int tspIterative(int mask, int pos, int n)
{
struct MyStructure
{
int mask;
int pos;
int n;

int new_result;
int result;

int stage;
};


int retVal = 0;

stack<MyStructure> myStack;

MyStructure currentStruct;
currentStruct.mask = mask;
currentStruct.pos = pos;
currentStruct.n = n;

currentStruct.new_result = 0;
currentStruct.result = 2147483647;

currentStruct.stage = 0;

myStack.push(currentStruct);


while (myStack.empty() != true)
{
currentStruct = myStack.top();
myStack.pop();

switch (currentStruct.stage)
{
case 0:
if (currentStruct.mask == VISITED_ALL)
{
retVal = dist[pos][0];
continue;
}
else
{
currentStruct.stage = 1;
myStack.push(currentStruct);

for (int i = 0; i < currentStruct.n; i++)
{
if ((currentStruct.mask & (1 << i)) == 0)
{
MyStructure newStruct;
newStruct.mask = currentStruct.mask | (1 << i);
newStruct.pos = i;
newStruct.n = currentStruct.n;

newStruct.result = currentStruct.result;
newStruct.new_result = currentStruct.new_result;

newStruct.stage = 0;

myStack.push(newStruct);
}
}
continue;
break;

case 1:

for (int i = 0; i < currentStruct.n; i++)
{

if ((currentStruct.mask & (1 << i)) == 0)
{
currentStruct.new_result = dist[currentStruct.pos][i] + retVal;
currentStruct.result = min(currentStruct.result, currentStruct.new_result);
retVal = currentStruct.result;
}

}
continue;
break;

}

}

return retVal;
}

}

最佳答案

好的,我已经知道了。如果有人感兴趣,这是我的解决方案。

int tspIterative(int mask, int pos, int n)
{
struct MyStructure
{
int mask;
int pos;
int n;

int new_result;
int result;

int nextPos;

int stage;
};


int retVal = 0;
int k = 0;

stack<MyStructure> myStack;

MyStructure currentStruct;
currentStruct.mask = mask;
currentStruct.pos = pos;
currentStruct.n = n;

currentStruct.new_result = 0;
currentStruct.result = 2147483647;

currentStruct.nextPos = 0;

currentStruct.stage = 0;

myStack.push(currentStruct);


while (myStack.empty() != true)
{
currentStruct = myStack.top();
myStack.pop();

switch (currentStruct.stage)
{

case 0:
{
jump:
if (currentStruct.mask == VISITED_ALL)
{
retVal = dist[currentStruct.pos][0];
continue;
}

else
{
currentStruct.stage = 1;
myStack.push(currentStruct);

for (int i = currentStruct.nextPos + k; i < currentStruct.n; i++)
{
if ((currentStruct.mask & (1 << i)) == 0)
{
MyStructure newStruct;
newStruct.mask = (currentStruct.mask) | (1 << i);
newStruct.pos = i;
newStruct.n = currentStruct.n;

newStruct.result = 2147483647;
newStruct.new_result = 0;

newStruct.nextPos = 0;

newStruct.stage = 0;

currentStruct = myStack.top();
myStack.pop();
currentStruct.nextPos = i;
myStack.push(currentStruct);


myStack.push(newStruct);
break;
}
}
continue;
}

break;
}
case 1:
{
k = 0;
for (int i = currentStruct.nextPos + k; i < currentStruct.n; i++)
{

if ((currentStruct.mask & (1 << i)) == 0)
{
if (k > 0)
{
goto jump;
}
currentStruct.new_result = dist[currentStruct.pos][i] + retVal;
currentStruct.result = min(currentStruct.result, currentStruct.new_result);
retVal = currentStruct.result;
k = 1;
}

}
continue;
break;
}
}
}


return retVal;
}

关于c++ - TSP递归解决方案的迭代形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62817740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com