帮我看看我的"骑士旅行"还能怎么改进.

conchen 2003-10-19 02:04:26
#include <iostream.h>
#include <iomanip.h>
#include <conio>

const int horizontal[] = { 2, 1, -1, -2, -2, -1, 1, 2 } ;
const int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 } ;
const int arraySize = 8 ;
int board[ arraySize ][ arraySize ] = { 0 } ;
int accessibility[ arraySize ][ arraySize ] =
{ { 2, 3, 4, 4, 4, 4, 3, 2 }, { 3, 4, 6, 6, 6, 6, 4, 3 },
{ 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 },
{ 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 },
{ 3, 4, 6, 6, 6, 6, 4, 3 }, { 2, 3, 4, 4, 4, 4, 3, 2 } } ;

int currentRow, currentColumn, counter = 1 ;

void choose( int &, int & ) ;
void printBoard( void ) ;

int main()
{
cout << "Input the start row: " ;
cin >> currentRow ;
cout << "Input the start column: " ;
cin >> currentColumn ;
board[ currentRow ][ currentColumn ] = counter ;

choose( currentRow, currentColumn ) ;

getch();
return 0 ;
}

void choose( int &row, int &col )
{
int best = 9, tempInt, num, tempRow, tempCol ;
bool canTravel = false ;
for ( int i = 0; i <= 7; i++ ) {
tempRow = row + horizontal[ i ] ;
tempCol = col + vertical[ i ] ;
tempInt = accessibility[ tempRow ][ tempCol ] ;
if ( tempRow < 0 || tempRow > 7 || tempCol < 0 || tempCol > 7 ) continue ;
if ( tempInt < best && 0 == board[ tempRow ][ tempCol ] ) {
best = tempInt ;
num = i ;
canTravel = true ;
}
}
if ( canTravel ) {
row += horizontal[ num ] ;
col += vertical[ num ] ;
if ( counter ++ <= 64 ) {
board[ row ][ col ] = counter ;
choose( row, col ) ;
}
}
else printBoard();
}

void printBoard( void )
{
for ( int i = 0; i < arraySize; i++ ) {
for ( int j = 0; j < arraySize; j++ ) {
cout.fill( '0' ) ;
cout << setw( 2 ) << board[ i ][ j ] << " " ;
}
cout << endl ;
}
cout << endl ;
}


...全文
40 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
conchen 2003-10-20
  • 打赏
  • 举报
回复
只改变了几个
改变 后 前
55: 62 - 58
56: 60 - 57
62: 56 - 57
63: 59 - 57
66: 59 - 62
71: 58 - 56
74: 61 - 54
conchen 2003-10-20
  • 打赏
  • 举报
回复
改进了一点,但代码也长了很多.


#include <iostream.h>
#include <iomanip.h>
#include <conio>

const int horizontal[] = { 2, 1, -1, -2, -2, -1, 1, 2 } ;
const int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 } ;
const int arraySize = 8 ;
int board[ arraySize ][ arraySize ] = { 0 } ;
int accessibility[ arraySize ][ arraySize ] =
{ { 2, 3, 4, 4, 4, 4, 3, 2 }, { 3, 4, 6, 6, 6, 6, 4, 3 },
{ 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 },
{ 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 },
{ 3, 4, 6, 6, 6, 6, 4, 3 }, { 2, 3, 4, 4, 4, 4, 3, 2 } } ;

int currentRow, currentColumn, counter = 1 ;

void choose( int &, int & ) ;
void printBoard( void ) ;

int main()
{
cout << "Input the start row: " ;
cin >> currentRow ;
cout << "Input the start column: " ;
cin >> currentColumn ;
board[ currentRow ][ currentColumn ] = counter ;

choose( currentRow, currentColumn ) ;
cout << "counter = " << counter << endl ;
getch();
return 0 ;
}

void choose( int &row, int &col )
{
int best = 9, largest, tempInt, num, tempRow, tempCol ;
int a[ 2 ][ 8 ] = { 0 }, j = 0 ;
bool canTravel = false ;
for ( int i = 0; i <= 7; i++ ) {
tempRow = row + horizontal[ i ] ;
tempCol = col + vertical[ i ] ;
tempInt = accessibility[ tempRow ][ tempCol ] ;
if ( tempRow < 0 || tempRow > 7 || tempCol < 0 || tempCol > 7 ) continue ;
if ( tempInt < best && 0 == board[ tempRow ][ tempCol ] ) {
best = tempInt ;
num = i ;
canTravel = true ;
}
}
for ( int i = 0; i <= 7; i ++ ) {
tempRow = row + horizontal[ i ] ;
tempCol = col + vertical[ i ] ;
if ( tempRow < 0 || tempRow > 7 || tempCol < 0 || tempCol > 7 ) continue ;
tempInt = accessibility[ tempRow ][ tempCol ] ;
if ( tempInt == best && board[ tempRow ][ tempCol ] == 0 ) {
a[ 0 ][ j ] = i ;
if ( ( tempCol + 1 ) <= 7 && board[ tempRow ][ tempCol + 1 ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempCol - 1 ) >= 0 && board[ tempRow ][ tempCol - 1 ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempRow - 1 ) >= 0 && board[ tempRow - 1 ][ tempCol ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempRow - 1 ) >= 0 && ( tempCol - 1 ) >= 0 && board[ tempRow - 1 ][ tempCol - 1 ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempRow - 1 ) >= 0 && ( tempCol + 1 ) <= 7 && board[ tempRow - 1 ][ tempCol + 1 ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempRow + 1 ) <= 7 && board[ tempRow + 1 ][ tempCol ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempRow + 1 ) <= 7 && ( tempCol - 1 ) >= 0 && board[ tempRow + 1 ][ tempCol - 1 ] != 0 )
a[ 1 ][ j ] ++ ;
else if ( ( tempRow + 1 ) <= 7 && ( tempCol + 1 ) <= 7 && board[ tempRow + 1 ][ tempCol + 1 ] != 0 )
a[ 1 ][ j ] ++ ;
j ++ ;
}
}

largest = a[ 1 ][ 0 ] ;
num = a[ 0 ][ 0 ] ;
for ( int k = 1; k < j; k ++ )
if ( a[ 1 ][ k ] > largest ) {
largest = a[ 1 ][ k ] ;
num = a[ 0 ][ k ] ;
}

if ( canTravel ) {
row += horizontal[ num ] ;
col += vertical[ num ] ;
if ( counter ++ <= 64 ) {
board[ row ][ col ] = counter ;
choose( row, col ) ;
}
}
else printBoard();
}

void printBoard( void )
{
for ( int i = 0; i < arraySize; i++ ) {
for ( int j = 0; j < arraySize; j++ ) {
cout.fill( '0' ) ;
cout << setw( 2 ) << board[ i ][ j ] << " " ;
}
cout << endl ;
}
cout << endl ;
}
ttlb 2003-10-19
  • 打赏
  • 举报
回复
up

69,335

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧