1
我的程序中的循环配置方式有问题。在做了一些调试之后,我发现循环一直运行直到最后一次迭代,恰好在temp与目标匹配之前。抛出EXC_BAD_ACCESS (code=1, address=0x0)
,程序退出。 (11)无法迭代通过STL设置
bool isadjacent(string& a, string& b)
{
int count = 0;
int n = a.length();
for (int i = 0; i < n; i++)
{
if (a[i] != b[i]) count++;
if (count > 1) return false;
}
return count == 1 ? true : false;
}
int shortestChainLen(string& start, string& target, set<string> &D)
{
queue<QItem> Q;
QItem item = {start, 1};
Q.push(item);
while (!Q.empty())
{
QItem curr = Q.front();
Q.pop();
for (set<string>::iterator it = D.begin(); it != D.end(); it++)
{
string temp = *it;
if (isadjacent(curr.word, temp))
{
item.word = temp;
item.len = curr.len + 1;
Q.push(item);
D.erase(temp);
if (temp == target)
return item.len;
}
}
}
return 0;
}
感谢您的快速响应。我一直在为此工作数小时,我不能相信我错过了它。 – Malcolm