Last active
November 19, 2015 02:07
-
-
Save shoken0x/484c468cbb5946065103 to your computer and use it in GitHub Desktop.
PCC Book: 深さ優先探索(DFS: Depth-First Search)
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
# input | |
$n = 4 | |
$a = [1, 2, 4, 7] | |
$k = 13 | |
# 配列aからいくつか選び、その和をちょうどkにすることが | |
# できるかを求める | |
def dfs(i, sum) | |
return sum == $k if i == $n | |
return true if dfs(i + 1, sum) | |
return true if dfs(i + 1, sum + $a[i]) | |
return false | |
end | |
def slove | |
puts result = dfs(0, 0) ? 'Yes' : 'No' | |
end | |
slove |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment