結果

問題 No.3 ビットすごろく
ユーザー mafuyu-akimafuyu-aki
提出日時 2017-04-09 23:10:16
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,735 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 27,412 KB
実行使用メモリ 809,584 KB
最終ジャッジ日時 2023-09-25 01:33:01
合計ジャッジ時間 2,321 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;

}

0