Skip to content

Instantly share code, notes, and snippets.

@IAFahim
Created February 9, 2022 15:47
Show Gist options
  • Save IAFahim/7cb6cf792fe1d8080863dc1eae0c7fc7 to your computer and use it in GitHub Desktop.
Save IAFahim/7cb6cf792fe1d8080863dc1eae0c7fc7 to your computer and use it in GitHub Desktop.
find path
type Dfs struct {
dX []int
dY []int
grid [][]byte
find byte
obstacle byte
found bool
}
func newDfs(grid [][]byte, find byte, obstacle byte) *Dfs {
dX := []int{1, 1, 1, 0, 0, -1, -1, -1}
dY := []int{1, 0, -1, 1, -1, 1, 0, -1}
return &Dfs{dX: dX, dY: dY, grid: grid, find: find, obstacle: obstacle, found: false}
}
func (d *Dfs) possible(x, y int) {
for i := 0; i < len(d.dX); i++ {
_y := y + d.dY[i]
_x := x + d.dX[i]
if 0 <= _y && _y < len(d.grid) && 0 <= _x && _x < len(d.grid[_y]) && d.grid[_y][_x] == d.find {
if d.reached(_x, _y) {
return
}
d.grid[_y][_x] = d.obstacle
d.possible(_x, _y)
}
}
}
func (d *Dfs) reached(_x, _y int) bool {
return _y == len(d.grid)-1 && _x == len(d.grid[_y])-1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment