Problem solution · Go

Codeforces 1948C — Arrow Path

Codeforces 1948C — Arrow Path: a Go solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Depth-first search
Source
EndlessCheng Codeforces Go
Length
46 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Codeforces 1948C — Arrow Path, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 46 lines of Go from the credited upstream file 1948C.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.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1948C — Arrow Path · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") func cf1948C(in io.Reader, out io.Writer) {	dir4 := []struct{ x, y int }{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}	var T, m int	for Fscan(in, &T); T > 0; T-- {		Fscan(in, &m)		g := [2]string{}		Fscan(in, &g[0], &g[1])		vis := [2][]bool{make([]bool, m), make([]bool, m)}		var dfs func(int, int) bool		dfs = func(i, j int) bool {			if i == 1 && j == m-1 {				return true			}			vis[i][j] = true			for _, d := range dir4 {				x, y := i+d.x, j+d.y				if 0 <= x && x < 2 && 0 <= y && y < m {					if g[x][y] == '<' {						y--					} else {						y++					}					if !vis[x][y] && dfs(x, y) {						return true					}				}			}			return false		}		if dfs(0, 0) {			Fprintln(out, "YES")		} else {			Fprintln(out, "NO")		}	}} //func main() { cf1948C(bufio.NewReader(os.Stdin), os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗