1
好的,所以,首先,我不知道我在迭代深化中做了什麼。我一直在努力讓這段代碼能夠工作,但我不能。我在網上查找並找不到任何有關C++中的搜索的參考。迭代深化在C++中搜索
void Graph::IDS(int x, int required, int depth = 1)
{
if(x == required) return;
cout << "Iterated Deepening Search for " << required << ", starting from vertex " << x << " : " << endl;
IDS_util(x, required, depth);
cout << endl;
}
void Graph::IDS_util(int x, int required, int depth)
{
stack s;
bool *visited = new bool[n+1];
int i, j, k;
for(i = 0; i <= n; i++)
visited[i] = false;
cout << "Depth = " << depth << ": ";
visited[x] = true;
for (int c = 1; c <= n; c++){
s.push(x);
if(isConnected(x, c) && !visited[c])
{
for (j = 0; j < depth; j++){
k = s.pop();
if(k == required) return;
cout << "[" << k <<"] ";
for (i = n; i >= 0 ; --i)
if (isConnected(k, i) && !visited[i]) {
s.push(i);
visited[i] = true;
}
}
}
}
if(depth == n) return;
cout << endl;
IDS_util(x, required, depth+1);
}
從鄰接矩陣的輸出:
0,1,1,1,0,0,0,0,0
0,0,0,0,1,0,0,0,0
0,0,0,0,0,1,1,0,0
0,0,0,0,0,0,0,1,0
0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
這是本圖的定向的版本:
[1]
/| \
[2] [3] [4]
//\ \
[5] [6] [7] [8]
/
[9]
是:
Iterated Deepening Search for 7, starting from vertex 1 :
Depth = 0:
Depth = 1: [1]
Depth = 2: [1] [2]
Depth = 3: [1] [2] [5]
Depth = 4: [1] [2] [5] [9]
Depth = 5: [1] [2] [5] [9] [3]
Depth = 6: [1] [2] [5] [9] [3] [6]
Depth = 7: [1] [2] [5] [9] [3] [6]
我理論上什麼知道搜索應該在做,我可以稍微說出我的搜索在做什麼,但我無法弄清楚如何解決它。任何人都可以提供的幫助將深受讚賞。
你可以使用遞歸嗎? – GriffinG
是的,絕對 – SamWN