一键导入
stack-based-backtrack
For search with undo: explicit decision stack, backtracking when paths fail, depth-first exploration with state restoration.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
For search with undo: explicit decision stack, backtracking when paths fail, depth-first exploration with state restoration.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Conducts iterative deep research on any topic using web search, progressive exploration, and structured synthesis. Use when asked for comprehensive research, deep investigation, thorough analysis, or multi-source exploration of any topic. Triggers: research, investigate, deep dive, comprehensive analysis, explore thoroughly, find everything about.
For cross-cutting concerns: add behavior without modifying functions, caching, timing, logging, validation wrappers.
For performance work: measure before changing, profile to find bottlenecks, compare before and after.
For symbolic computation: ASTs, mathematical expressions, code that manipulates code structure, expression transformations.
For ordered processing: A* search, Dijkstra, event simulation, task scheduling. Efficient min/max extraction with heap-based queue.
For dynamic programming: overlapping subproblems, recursive solutions with repeated computations, memoization to avoid redundant work.
| name | stack-based-backtrack |
| description | For search with undo: explicit decision stack, backtracking when paths fail, depth-first exploration with state restoration. |
Maintain explicit stack of decisions; pop and undo on failure.
def search_with_backtrack(initial_state):
"""DFS with explicit stack for backtracking."""
stack = [(initial_state, get_choices(initial_state))]
while stack:
state, choices = stack[-1]
if is_goal(state):
return state
if not choices:
# Backtrack: no more choices at this level
stack.pop()
if stack:
undo_last_choice(stack[-1][0])
continue
# Try next choice
choice = choices.pop()
new_state = apply_choice(state, choice)
if is_valid(new_state):
stack.append((new_state, get_choices(new_state)))
return None # No solution
# pal2.py - Panama palindrome search
class Panama:
def search(self, steps=50000000):
"""Depth-first search with explicit backtrack stack."""
for _ in range(steps):
if not self.stack:
return 'done'
action, direction, substr, arg = self.stack[-1]
if action == 'added':
# Undo the addition
self.remove(direction, arg)
elif action == 'trying':
if arg: # More candidates to try
word = arg.pop()
self.add(direction, word)
self.consider_candidates()
else: # Exhausted candidates
self.stack.pop()
return 'incomplete'
def consider_candidates(self):
"""Push new choice points onto stack."""
substr = self.get_target_substring()
direction = 'left' if self.diff < 0 else 'right'
candidates = self.find_candidates(substr, direction)
if candidates:
self.stack.append(('trying', direction, substr, candidates))
# Conceptual example: N-Queens
def solve_queens(n):
"""Place N queens with backtracking."""
stack = [([], list(range(n)))] # (placed, available_rows)
while stack:
placed, available = stack[-1]
if len(placed) == n:
return placed # Solution found
if not available:
stack.pop() # Backtrack
continue
row = available.pop()
col = len(placed)
if is_safe(placed, row, col):
new_placed = placed + [row]
new_available = [r for r in range(n)
if r not in new_placed and is_safe(new_placed, r, col+1)]
stack.append((new_placed, new_available))
return None