在到达边界后,返回 i = 9 的for 中,取回 book[9] , 返回 上一个节点 ,在这时 i 变成 0 了 ,不仅没有取回 book[8] ,而且 不会再进行第九次for循环了 ,这我实在是不知道原因 求解答 那些 cout 是程序出错后我 为了看清楚细节 加上的
class dfs
{
private:
int step[10] ;
int book[10] = {0,0,0,0,0,0,0,0,0,0};
int i;
public:
void func(int s)
{
cout << "zai di " << s << " ge he zi qian" << endl ;
if (s == 10)
{
if (100*step[1]+10*step[2]+step[3]+100*step[4]+10*step[5]+step[6] == 100*step[7]+10*step[8]+step[9])
{
cout << step[1];step[2];step[3] ;
cout << "+" ;
cout << step[4];step[5];step[6] ;
cout << "=";
cout << step[7];step[8];step[9] ;
cout << endl;
}
cout << "fan hui di jiu ge he zi " << endl;
return;
}
for (i=1;i<=9;i++)
{
if(book[i]==0)
{
step[s]=i;
cout << s << " hezi fangle " << i << "kapian "<< endl ;
book[i]=1;
cout << "jin xing di" << s+1 << "ge he zi " << endl;
func(s+1);
book [i]=0;
cout << "****************************************" << i << endl;
cout << "qv hui pai " << i << endl ;
}
}
cout << "fan hui di" << s-1 << " ge he zi "<< endl;
return;
}
};