problem = search_problem do start [:seed_list, seeds] goal? { false } transitions do |state| if state[0] == :seed_list state[1] else y, x = state DIRS.filter_map { |dy,dx| # 範囲チェック・通行可判定 ... } end end valid? do |state| state[0].is_a?(Integer) && passable[[state[0], state[1]]] end end problem.solve_with(:bfs).visit_order end frontier = free_closure([[0,0]], ...) seen = {}; punches = 0 loop do break puts punches if seen[goal] 💎 素Ruby版 (01-BFS) class Deque # 自前 deque (push_front/back, pop_fro end dist = Array.new(H) { Array.new(W, INF) dist[0][0] = 0 queue = Deque.new queue.push_front([[0,0], 0]) until queue.empty? (y, x), c = queue.pop_front next if c != dist[y][x] # コスト0: 4近傍の通行可マス DIRS.each do |dy, dx| ny, nx = y+dy, x+dx # ... dist 更新 + push_front end 範 🐍 Python版 (k回ラウンド) count = [[INF]*W for _ in range(H)] count[0][0] = 0 for k in range(H * W): # 距離 k の点から DFS で通行可を広げる todo = deque( (i,j) for i in range(H) for j i if count[i][j] == k ) while todo: vi, vj = todo.pop() for di, dj in [[0,-1],[0,1],[-1 wi, wj = vi+di, vj+dj # ... count[wi][wj] = k に更 # 距離 k+1 を 5×5 範囲で更新 for (i, j) in [...]: i DSL版だけ 外側のループ が DSLの外に漏れる。 「何を解くか」だけ書きたかったのに、結局手続きを書く羽目に