結果

問題 No.4 おもりと天秤
ユーザー Takuya ItoTakuya Ito
提出日時 2023-12-08 15:48:59
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,516 bytes
コンパイル時間 11,247 ms
コンパイル使用メモリ 223,596 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-08 15:49:15
合計ジャッジ時間 12,291 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
6,676 KB
testcase_07 WA -
testcase_08 AC 1 ms
6,676 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1 ms
6,676 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import "fmt"

// メモ化用のキャッシュ
var memo map[[3]int]bool

// 天秤が水平になるおもりの組み合わせが存在するかどうかを判定する関数
func findBalance(weights []int, leftSum, rightSum, index int) bool {
	// メモがあれば結果を返す
	if result, found := memo[[3]int{leftSum, rightSum, index}]; found {
		return result
	}

	// 全てのおもりを使用した場合、左右の合計が等しいかどうかを確認
	if index == len(weights) {
		result := leftSum == rightSum
		// メモに保存
		memo[[3]int{leftSum, rightSum, index}] = result
		return result
	}

	// 現在のおもりを左に置いた場合
	if findBalance(weights, leftSum+weights[index], rightSum, index+1) {
		// メモに保存
		memo[[3]int{leftSum, rightSum, index}] = true
		return true
	}

	// 現在のおもりを右に置いた場合
	if findBalance(weights, leftSum, rightSum+weights[index], index+1) {
		// メモに保存
		memo[[3]int{leftSum, rightSum, index}] = true
		return true
	}

	// どちらにも置かない場合
	// メモに保存
	memo[[3]int{leftSum, rightSum, index}] = false
	return false
}

func main() {
	// おもりの重さのリスト
	weights := []int{1, 2, 3, 4, 5}

	// メモを初期化
	memo = make(map[[3]int]bool)

	// すべてのおもりを使用して天秤が水平になる組み合わせがあるかどうかを判定
	if findBalance(weights, 0, 0, 0) {
		fmt.Println("possible")
	} else {
		fmt.Println("impossible")
	}
}
0