結果

問題 No.3 ビットすごろく
ユーザー tsuchinagatsuchinaga
提出日時 2019-03-09 01:21:09
言語 Go
(1.22.1)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 927 bytes
コンパイル時間 13,469 ms
コンパイル使用メモリ 209,788 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:13:14
合計ジャッジ時間 15,158 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n int
	_, _ = fmt.Scan(&n)
	dest := make(map[int][]int, 0) // あるマスから行ける移動先
	for i := 1; i <= n; i++ {
		b := strings.Count(fmt.Sprintf("%b", i), "1")
		d := make([]int, 0)
		if i+b <= n {
			d = append(d, i+b)
		}
		if i-b >= 1 {
			d = append(d, i-b)
		}
		dest[i] = d
	}
	// fmt.Println(dest)

	type Node struct {
		n, depth int
	}
	stack := []Node{{1, 1}}
	// 幅優先で最初に見つかった答えが最短
	ans := -1
	for {
		if len(stack) == 0 { // 探索し終えた
			break
		}

		p := stack[0] // 現在地
		if p.n == n {
			ans = p.depth
			break
		}

		stack = stack[1:] // pop
		for _, d := range dest[p.n] {
			if _, ok := dest[d]; ok {
				stack = append(stack, Node{d, p.depth + 1})
			}
		}

		// 一度通ったところはdestから消す
		delete(dest, p.n)
		// fmt.Println(p, stack, dest)
	}

	fmt.Println(ans)
}
0