結果

問題 No.3 ビットすごろく
ユーザー Dakatan
提出日時 2019-06-11 13:48:29
言語 Go
(1.23.4)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 722 bytes
コンパイル時間 11,604 ms
コンパイル使用メモリ 220,540 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:21:51
合計ジャッジ時間 12,636 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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