[Leetcode] DFS

DFS

576. Out of Boundary Paths

576. Out of Boundary Paths

approach-1 :Breadth-First-Search(TLE)

I have no idea why this question will be categorized as DFS. I think the straight-forward solution is using BFS. The node can be represented as the state of each moment, like the node’s location(i, j) and its used steps. The queue can store node for each moment and using BFS to search each available moment. But it may cost a lot of time so it can’t pass.

The code like that:

class Solution {
public:
    struct Node {
        int i, j;
        int times;
        Node(int _i, int _j, int _times): i(_i), j(_j), times(_times) {}
    };

    bool validate(int m, int n, int i, int j) {
        return !(i < 0 || j < 0 || i == m || j == n);
    }

    int findPaths(int m, int n, int N, int i, int j) {
        Node start(i, j, 0);
        queue<Node> nodeQueue;
        nodeQueue.push(start);
        int total = 0;
        int threshold = 1000000000 + 7;
        while (!nodeQueue.empty()) {
            Node tmp = nodeQueue.front();
            nodeQueue.pop();
            if (tmp.times > N) { continue; }
            if (validate(m, n, tmp.i, tmp.j)) {
                if (tmp.times == N) { continue; }
                nodeQueue.push(Node(tmp.i, tmp.j + 1, tmp.times + 1));
                nodeQueue.push(Node(tmp.i, tmp.j - 1, tmp.times + 1));
                nodeQueue.push(Node(tmp.i + 1, tmp.j, tmp.times + 1));
                nodeQueue.push(Node(tmp.i - 1, tmp.j, tmp.times + 1));
            } else {
                total += 1;
                total %= threshold;
            }
        }
        return total;
    }
};

Complexity Analysis

  • Time Complexity: O( n^4 )
  • Space Complexity: O( n )

approach-2: Dynamic Programming (Accepted)

Using dynamic programming will work. We can imagine that for each moment, it can be move from its adjacent moment. The equation will look like that:

dp[i][j][step]=dp[i−1][j][step - 1]+dp[i+1][j][step - 1]
            +dp[i][j−1][step - 1]+dp[i][j+1][step - 1]

What’s more, dp[0][j][step] will be equal to 1. Because it can only go left for one step, and then it will have to go towards another direction.

The code will look like that:

class Solution {
public:

    int findPaths(int m, int n, int N, int i, int j) {
    uint array[51][51][51] = {}; // It have to be as type of uint  or long.
    int threshold = 1000000007;

    for (int step = 1; step <= N; step++) {
        for (int mi = 0; mi < m; mi++) {
            for (int ni = 0; ni < n; ni++) {
                array[mi][ni][step] = ((mi == 0 ? 1 : array[mi - 1][ni][step - 1]) % threshold 
                                     + (mi == m - 1 ? 1 : array[mi + 1][ni][step - 1]) % threshold
                                     + (ni == 0 ? 1 : array[mi][ni - 1][step - 1]) % threshold
                                     + (ni == n - 1 ? 1 : array[mi][ni + 1][step - 1]) % threshold) % threshold;
            }
        }
    }
    return array[i][j][N];
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章