結果

問題 No.3 ビットすごろく
ユーザー mafuyu-akimafuyu-aki
提出日時 2017-04-10 20:56:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,581 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 27,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 00:38:36
合計ジャッジ時間 1,564 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 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 Q[10000][2]={0};
	int count = 1;

	int P = 1; //今いる場所
	int i = -1; //追加するキューのシーケンス
	int j = 0;
	char map[10000+1] = {0};
	int ido = 0;

	map[1] = 1;
	for( ; P < N;  j++ )
	{
		ido = bit( P );
		if( P + ido <= N && map[ P + ido ] == 0 )
		{
			i++;
			Q[i][0] = P+ ido;
			Q[i][1] = count+1;
			map[ P + ido ] = 1;
		}

		if( P - ido > 0 && map[ P - ido ] == 0 )
		{
			i++;
			Q[i][0] = P - ido;
			Q[i][1] = count+1;
			map[ P - ido ] = 1;
		}

		if( Q[j][0] == 0 )
			break;

		P = Q[j][0];
		count = Q[j][1];

	}

	if( P != N )
		count = -1;
	printf( "%d\n", count );

	
	return 0;

}

0