https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/
func exist(board [][]byte, word string) bool { n := len(word) r := len(board) c := len(board[0]) if n > r*c { return false } g := func(p, q, a, b int) bool { return (p == a && (q-b == 1 || q-b == -1)) || ((p-a == 1 || p-a == -1) && q == b) } for i := 0; i < n; i++ { v := word[i] p, q, a, b := -1, -1, -1, -1 f := false for j := 0; j < r; j++ { for k := 0; k < c; k++ { u := board[j][k] if v == u { p, q = j, k if a != -1 { if !g(p, q, a, b) { return false } } a, b = p, q f = true break } } } if !f { return true } } return true }