結果

問題 No.3 ビットすごろく
ユーザー marimom7marimom7
提出日時 2015-10-11 13:16:27
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 934 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 65,312 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 07:34:21
合計ジャッジ時間 1,617 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>

using namespace std;

const int MAX = 10000;

int BitCount(int number,int serch_bitnum){
	int counter = 0;
	for(int i = 0; i < serch_bitnum; i++)if(number & (1 << i))counter++;
	return counter;
}

bool Found(int *meta,int data){
	return meta[data] != 0;
}

int main(){

	int N;
	queue<int> que;
	int cellcounter[MAX+1] = {0};
	int cellcount = -1;
	int cell = -1;
	int step = -1;

	cin >> N;

	que.push(1);
	cellcounter[1] = 1;
	cellcount = 0;

	do{
		cell = que.front();
		que.pop();
		step = BitCount(cell,16);

		cellcount = cellcounter[cell];		

		if(!Found(cellcounter,cell+step) && cell+step < N+1){
			que.push(cell+step);
			cellcounter[cell+step] = cellcount+1;
		}

		if(!Found(cellcounter,cell-step) && cell-step > 0){
			que.push(cell-step);
			cellcounter[cell-step] = cellcount+1;
		}
		

	}while(!que.empty());

	cout << (cellcounter[N] ? cellcounter[N] : -1) << endl;

	return 0;
}
0