[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];
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值