Problem solution · Go

Codeforces 1225E — Rock Is Push

Codeforces 1225E — Rock Is Push: 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
56 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 1225E — Rock Is Push, 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

  • 56 lines of Go from the credited upstream file 1225E.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 1225E — Rock Is Push · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc CF1225E(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	const mod int64 = 1e9 + 7 	var n, m int	Fscan(in, &n, &m)	if n == 1 && m == 1 {		Fprint(out, 1)		return	}	a := make([]string, n)	for i := range a {		Fscan(in, &a[i])	} 	r := make([][]int, n+1)	c := make([][]int, n+1)	dpr := make([][]int64, n+1) // dp 前缀和	dpc := make([][]int64, n+1)	for i := range r {		r[i] = make([]int, m+1)		c[i] = make([]int, m+1)		dpr[i] = make([]int64, m+1)		dpc[i] = make([]int64, m+1)	}	for i := n - 1; i >= 0; i-- {		for j := m - 1; j >= 0; j-- {			r[i][j] = r[i][j+1]			c[i][j] = c[i+1][j]			if a[i][j] == 'R' {				r[i][j]++				c[i][j]++			}			if i == n-1 && j == m-1 {				dpc[i][j] = 1				dpr[i][j] = 1			} else {				dpc[i][j] = (dpc[i+1][j] + dpr[i][j+1] - dpr[i][m-r[i][j+1]]) % mod				dpr[i][j] = (dpr[i][j+1] + dpc[i+1][j] - dpc[n-c[i+1][j]][j]) % mod			}		}	}	Fprint(out, ((dpc[0][0]-dpc[1][0]+dpr[0][0]-dpr[0][1])%mod+mod)%mod)} //func main() { CF1225E(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 ↗