#include #include #include #define READ_BUFSIZE ( 16 ) #define READ_DELIMITER ( " " ) //標準入力取得(1行) int GetStdin( char* pszStr, int lMaxLen ) { int lLen = 0; memset( pszStr, 0, lMaxLen ); if( fgets( pszStr, lMaxLen, stdin ) ) { lLen = strlen( pszStr ); if( lLen >= 1 ) { if( pszStr[ lLen - 1 ] == 0x0A ) { pszStr[ lLen - 1 ] = 0; lLen--; } } } return( lLen ); } int bit( int value ) { int count = 0; for( int i = 1; i <= 10000; i*=2 ) { if( ( value & i ) == i ) count++; } return count; } //P : 今いる場所 最初1 //N:目的値 //c:これまでの移動回数 //map:通ってきた道 //count Nに到達して中で最小の値 int sugoroku( int P, int N, int c, int map[], int count ) { int map1[10000]; int map2[10000]; c++; if( P == N ) { if( count == 0 || c < count ) count = c; return( count ); } if( map[P] == 0 ) map[P] = 1; else { return( count ); } memcpy( map1, map, sizeof(int)*1000 ); memcpy( map2, map, sizeof(int)*1000 ); int ido = bit( P ); //printf( "P:%d bit:%d\n", P, ido ); //右に移動できるか確認 if( P + ido <= N ) { count = sugoroku( P + ido, N, c, map1, count ); } //左に移動できるか確認 if( P - ido > 0 ) { count = sugoroku( P - ido, N, c, map2, count ); } return count; } int main(int argc, char *argv[]) { char szRead[READ_BUFSIZE] = ""; //char* psOutbuf[10000] = { 0 }; int lInput[4] = {0}; GetStdin( szRead, READ_BUFSIZE ); int N = atoi( szRead ); int map[10000] = {0}; int count = sugoroku( 1, N, 0, map, 0 ); if( count == 0 ) count = -1; printf( "%d\n", count ); return 0; }