Problem solution · Go

Codeforces 1520G — To Go Or Not To Go?

Codeforces 1520G — To Go Or Not To Go?: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
99 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

Direct simulation

For Codeforces 1520G — To Go Or Not To Go?, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 99 lines of Go from the credited upstream file 1520G.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 1520G — To Go Or Not To Go? · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc CF1520G(_r io.Reader, out io.Writer) {	_i, buf := 1<<12, make([]byte, 1<<12)	rc := func() byte {		if _i == 1<<12 {			_r.Read(buf)			_i = 0		}		b := buf[_i]		_i++		return b	}	r := func() (x int) {		b := rc()		neg := false		for ; '0' > b; b = rc() {			if b == '-' {				neg = true			}		}		for ; '0' <= b; b = rc() {			x = x*10 + int(b&15)		}		if neg {			return -x		}		return	}	min := func(a, b int64) int64 {		if a < b {			return a		}		return b	}	const inf int64 = 1e18	type pair struct{ x, y int }	var dir4 = []pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} 	n, m, w := r(), r(), int64(r())	a := make([][]int, n)	dis := make([][]int64, n)	for i := range a {		a[i] = make([]int, m)		for j := range a[i] {			a[i][j] = r()		}		dis[i] = make([]int64, m)	} 	ans := inf	bfsAll := func(sx, sy int) int64 {		for i := range dis {			for j := range dis[i] {				dis[i][j] = -1			}		}		dis[sx][sy] = 0		q := []pair{{sx, sy}}		for curD := w; len(q) > 0; curD += w {			tmp := q			q = nil			for _, p := range tmp {				for _, d := range dir4 {					if x, y := p.x+d.x, p.y+d.y; 0 <= x && x < n && 0 <= y && y < m && a[x][y] >= 0 && dis[x][y] < 0 {						dis[x][y] = curD						q = append(q, pair{x, y})					}				}			}		}		if sx == 0 && dis[n-1][m-1] > 0 {			ans = dis[n-1][m-1]		}		mi := inf		for i, r := range a {			for j, v := range r {				if v > 0 && dis[i][j] >= 0 {					mi = min(mi, dis[i][j]+int64(v))				}			}		}		return mi	}	ans = min(ans, bfsAll(0, 0)+bfsAll(n-1, m-1))	if ans == inf {		ans = -1	}	Fprint(out, ans)} //func main() { CF1520G(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 ↗