函数递归为什么会出现不一样的结果????
程序代码:
#include <iostream>
#include <iomanip>
using namespace std;
const int n=5;
int move[9][3];
int l,m,a[n+1][n+1];
bool q;
void fun(int i,int j,int k,bool &q)
{
int v=0,g,h;
do
{
v++;
q=false;
g=i+move[v][1];
h=j+move[v][2];
if(g>=1&&h<=8&&h>=1&&h<=8&&a[g][h])
{
a[g][h]=k;
if(k<n*n)
{
fun(g,h,k+1,q);
if(!q) a[g][h]=0;
}
else
{
q=true;
}
}
}while(!q&&v!=8);
}
int main()
{
move[1][1]=2;move[1][2]=1;
move[2][1]=1;move[2][2]=2;
move[3][1]=-1;move[3][2]=2;
move[4][1]=-2;move[4][2]=1;
move[5][1]=-2;move[5][2]=-1;
move[6][1]=-1;move[6][2]=-2;
move[7][1]=1;move[7][2]=-2;
move[8][1]=2;move[8][2]=-1;
cout<<"n="<<n<<endl;
cout<<"the first position:";
int x1,y1;
cin>>x1>>y1;
fun(x1,y1,2,q);
if(q)
{
for(l=1;l<=n;l++)
{
for(m=1;m<=n;m++)
cout<<setw(4)<<a[l][m];
cout<<endl;
}
}
else
{
cout<<"no solutuon!"<<endl;
}
cout<<endl;
return 0;
}
为什么在输入1,1后会出现 no solution !的结果呢?很是不理解。。。另外请专家给讲解一下其中的递归函数是怎么运行的呢?







