Approach
Depth-first search
For Codeforces 377A — Maze, 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
- 53 lines of Go from the credited upstream file 377A.go.
- The implementation visibly relies on sequence storage.
- 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.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF377A(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 dir4 := []struct{ x, y int }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}15 16 var n, m, k int17 Fscan(in, &n, &m, &k)18 a := make([][]byte, n)19 for i := range a {20 Fscan(in, &a[i])21 }22 vis := make([][]bool, n)23 for i := range vis {24 vis[i] = make([]bool, m)25 }26 var dfs func(int, int)27 dfs = func(i, j int) {28 vis[i][j] = true29 for _, d := range dir4 {30 if x, y := i+d.x, j+d.y; 0 <= x && x < n && 0 <= y && y < m && !vis[x][y] && a[x][y] == '.' {31 dfs(x, y)32 }33 }34 if k > 0 {35 k--36 a[i][j] = 'X'37 }38 }39 for i, row := range a {40 for j, v := range row {41 if v == '.' {42 dfs(i, j)43 for _, r := range a {44 Fprintf(out, "%s\n", r)45 }46 return47 }48 }49 }50}51 5253