mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
47 lines
1.1 KiB
Ruby
47 lines
1.1 KiB
Ruby
=begin
|
|
File: permutations_i.rb
|
|
Created Time: 2024-05-22
|
|
Author: Xuan Khoa Tu Nguyen ([email protected])
|
|
=end
|
|
|
|
### バックトラッキング:すべての順列 I ###
|
|
def backtrack(state, choices, selected, res)
|
|
# 状態の長さが要素数と等しくなったら、解を記録する
|
|
if state.length == choices.length
|
|
res << state.dup
|
|
return
|
|
end
|
|
|
|
# すべての選択肢を走査する
|
|
choices.each_with_index do |choice, i|
|
|
# 枝刈り:同じ要素を繰り返し選んではならない
|
|
unless selected[i]
|
|
# 試行:選択を行い、状態を更新する
|
|
selected[i] = true
|
|
state << choice
|
|
# 次の選択に進む
|
|
backtrack(state, choices, selected, res)
|
|
# 復元:選択を取り消し、以前の状態に戻す
|
|
selected[i] = false
|
|
state.pop
|
|
end
|
|
end
|
|
end
|
|
|
|
### すべての順列 I ###
|
|
def permutations_i(nums)
|
|
res = []
|
|
backtrack([], nums, Array.new(nums.length, false), res)
|
|
res
|
|
end
|
|
|
|
### 実行用コード ###
|
|
if __FILE__ == $0
|
|
nums = [1, 2, 3]
|
|
|
|
res = permutations_i(nums)
|
|
|
|
puts "入力配列 nums = #{nums}"
|
|
puts "すべての順列 res = #{res}"
|
|
end
|