【LeetCode】Sword Pointer Offer <Second brush>(6)

Table of contents

Topic: Sword Points to Offer 12. Paths in the Matrix - LeetCode

Question interface:

Problem-solving ideas:

Code:

Passed! ! !

Topic: Sword Pointer Offer 13. Robot's range of motion - LeetCode

Question interface:

Problem-solving ideas:

Code:

Passed! ! !

Write at the end:


Topic: Sword Points to Offer 12. Paths in the Matrix - LeetCode

Question interface:

func exist(board [][]byte, word string) bool {

}

Problem-solving ideas:

This is a classic search question. Using and is a depth-first search. This method is the method I prefer to use. Let me talk about several core elements of this implementation method:

Let's look at it from top to bottom. The function of the dic array is to allow us to search for elements in four directions when searching;

The purpose of the vis array is to determine whether the grid is occupied during this search;

The check function is the characteristic implementation of golang. We implement the function logic within the main logic;

The last loop is the logic of searching using each grid as a starting point.

Code:

type pair struct {
    x int
    y int
}

var dic = []pair{
   
   {-1, 0}, {1, 0}, {0, -1}, {0, 1}}

func exist(board [][]byte, word string) bool {
    h, w := len(board), len(board[0])

    // 用于判断格子是否已经被占用
    vis := make([][]bool, h)
    for i := range vis {
        vis[i] = make([]bool, w)
    }

    // 搜索函数
    var check func(i, j, k int) bool
    check = func(i, j, k int) bool {
        if board[i][j] != word[k] { // 字符匹配失败
            return false
        }
        if k == len(word)-1 { // 单词匹配成功
            return true
        }
        vis[i][j] = true // 标记使用过的单元格
        defer func() { vis[i][j] = false }() // 回溯的时候还原使用过的单元格
        for _, dir := range dic {
            newI, newJ := dir.x+i, dir.y+j
            if newI >= 0 && newI < h && newI < h && newJ >= 0 && newJ < w && !vis[newI][newJ] {
                if  check(newI, newJ, k+1) {
                    return true
                }
            }
        }
        return false
    }

    // 每个格子作为起点搜素
    for i, row := range board {
        for j := range row {
            if check(i, j, 0) {
                return true
            }
        }
    }
    return false
}

Passed! ! !

Topic: The sword refers to Offer 13. The range of motion of the robot - LeetCode

Question interface:

func movingCount(m int, n int, k int) int {

}

Problem-solving ideas:

This question is still a search question, similar to the previous question. There are two main points. First, for this question we have to calculate the number of steps taken by the robot. The second point is that we need to find the sum of its digits to judge it. Whether the location can be reached.

Code:

func movingCount(m int, n int, k int) int {
	dp := make([][]int, m)
	for i := range dp {
		dp[i] = make([]int, n)
	}

	return dfs(m, n, 0, 0, k, dp)
}

func dfs(m, n, i, j, k int, dp [][]int) int {
	if i < 0 || j < 0 || i >= m || j >= n || dp[i][j] == 1 || (sumPos(i)+sumPos(j)) > k {
		return 0
	}

	dp[i][j] = 1

	sum := 1
	sum += dfs(m, n, i, j+1, k, dp)
	sum += dfs(m, n, i+1, j, k, dp)
	return sum
}

// 求所有位之和
func sumPos(n int) int {
	var sum int

	for n > 0 {
		sum += n % 10
		n = n / 10
	}

	return sum
}

Passed! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/132646285