Approach
Depth-first search
For ABC378 D — Count Simple Paths, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 55 lines of Python from the credited upstream file abc378_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def main():5 import sys6 7 sys.setrecursionlimit(10**8)8 9 input = sys.stdin.readline10 11 h, w, k = map(int, input().split())12 s = [list(input().rstrip()) for _ in range(h)]13 dxy = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)]14 dxy = dxy[:4]15 ans = 016 17 def dfs(y, x, count=0):18 if count == k:19 nonlocal ans20 ans += 121 return22 23 used[y][x] = True24 25 for dx, dy in dxy:26 ny, nx = y + dy, x + dx27 28 if not (0 <= ny < h):29 continue30 if not (0 <= nx < w):31 continue32 if s[ny][nx] == "#":33 continue34 if used[ny][nx]:35 continue36 37 dfs(ny, nx, count + 1)38 39 used[y][x] = False40 41 used = [[False for _ in range(w)] for _ in range(h)]42 43 for i in range(h):44 for j in range(w):45 if s[i][j] == "#":46 continue47 48 dfs(i, j)49 50 print(ans)51 52 53if __name__ == "__main__":54 main()55