結果

問題 No.3 ビットすごろく
コンテスト
ユーザー Dakatan
提出日時 2019-06-11 13:48:29
言語 Go
(1.26.1)
コンパイル:
env GOCACHE=/tmp go build _filename_
実行:
./Main
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 722 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 9,796 ms
コンパイル使用メモリ 285,888 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2026-03-22 06:41:03
合計ジャッジ時間 10,661 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	var n int
	fmt.Scan(&n)
	if n == 1 {
		fmt.Println(1)
		return
	}
	bfs := make([]int, 0)
	bfs = append(bfs, 1)
	distance, find := 1, false
	mp := make(map[int]int)
	mp[1] = 1
	for len(bfs) > 0 && !find {
		length := len(bfs)
		for i := 0; i < length && !find; i++ {
			cur := bfs[0]
			bfs = bfs[1:]
			count := bits.OnesCount(uint(cur))
			nexts := []int{cur + count, cur - count}
			for _, next := range nexts {
				if next == n {
					find = true
					break
				} else if next >= 1 && next < n && mp[next] == 0 {
					bfs = append(bfs, next)
					mp[next] = 1
				}
			}
		}
		distance++
	}
	if find {
		fmt.Println(distance)
	} else {
		fmt.Println(-1)
	}
}
0