Created
December 11, 2010 14:47
-
-
Save fumokmm/737402 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def complete = [] // 完了リスト | |
def nonComplete = [ [step:0, list:[4, 2, 10]] ] // 未完了リスト | |
def processedSet = [] as HashSet // 処理済みリスト | |
// 選択の組み合わせ | |
def selection = [0, 1, 2].with{ | |
[it, it].combinations()*.sort().unique().findAll{ | |
it.count(it[0]) != it.size() | |
} | |
} | |
// メインタスク | |
while(nonComplete) { | |
// println "未完了=[${nonComplete.size()}]件 処理済=[${processedSet.size()}]件" | |
if (nonComplete) { | |
def cont = nonComplete.head() | |
nonComplete = nonComplete.tail() | |
if(processedSet.add(cont.list)) { | |
def trans = selection.collect{ translocate(cont, it) }.grep{ it } | |
// 判定 | |
complete.addAll(trans.findAll{ it.list.count(0) == 2 }) | |
nonComplete.addAll(trans.findAll{ | |
return (complete && it.step >= complete*.step.min()) ? false : true | |
}) | |
} | |
} | |
} | |
// 結果出力 | |
println complete*.step.min() | |
/** 差し替え */ | |
def translocate(cont, select) { | |
def result = cont.clone() | |
result.list = cont.list.clone() | |
// 移し替え可能な選択なら | |
if (select.collect{ result.list[it] > 0 }.every{ it }) { | |
for (i in 0..<result.list.size()) { | |
if (select.contains(i)) | |
result.list[i]-- | |
else | |
result.list[i] += 2 | |
} | |
result.step++ | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment