我做的骑士移动问题,不好意思,运行了一夜,结果也没出来!我不过我感觉自己做的是对的!
#include <iostream.h>
#include <iomanip.h>
int board[ 8 ][ 8 ] = { 0 };
int horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int TStop = 1;
int MoveStop = 64;
int MoveKnight( int, int );
void PrintFun( void );
void Initialization( void );
int main()
{
MoveKnight( 3, 4 );
PrintFun();
return 0;
}
int MoveKnight( int CurrentRow, int CurrentColumn )
{
if ( ::TStop == 64 ){
PrintFun();
Initialization();
PrintFun();
return 0;
}
for ( int Stop = 0; Stop < 8; Stop++ ){
if (( CurrentColumn + ::horizontal[ Stop ] ) < 0 || ( CurrentColumn + ::horizontal[ Stop ] ) > 7||
( CurrentRow + ::vertical[ Stop ] ) < 0 || ( CurrentRow + ::vertical[ Stop ] ) > 7 ||
( ::board[ CurrentRow + ::vertical[ Stop ] ][ CurrentColumn + ::horizontal[ Stop ] ] != 0))
continue;
::board[ CurrentRow + ::vertical[ Stop ] ][ CurrentColumn + ::horizontal[ Stop] ] = ::TStop++;
PrintFun();
if ( MoveKnight( ( CurrentRow + ::vertical[ Stop ] ), (CurrentColumn + ::horizontal[ Stop] )) == 0 ){
::board[ CurrentRow ][ CurrentColumn ] = MoveStop--;
PrintFun();
return 0;
}
}
::board[ CurrentRow ][ CurrentColumn ] = 0;
::TStop--;
return 1;
}
void PrintFun( )
{
for ( int Row = 0; Row < 8; Row++ ){
for ( int Culumn = 0; Culumn < 8; Culumn++ ){
cout << setw( 3 ) << board[ Row ][ Culumn ];
}
cout << endl;
}
cout << "_____________________________________________" << endl;
return;
}
void Initialization( )
{
for ( int Row = 0; Row < 8; Row++ ){
for ( int Culumn = 0; Culumn < 8; Culumn++ ){
::board[ Row ][ Culumn ] = 0;
}
}
return;
}