題目連結

BFS 變形,走的一步其實是多步(一次走到底)

AC Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
#endif

/*
[[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]]
[0,4]
[4,4]
[[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]]
[0,4]
[3,2]
[[0,0,0,0,1,0,0],[0,0,1,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,1],[0,1,0,0,0,0,0],[0,0,0,1,0,0,0],[0,0,0,0,0,0,0],[0,0,1,0,0,0,1],[0,0,0,0,1,0,0]]
[0,0]
[8,6]
*/

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

// handle special cases first
// [], "", ...
// range of input?

struct Data {
    int x, y;

    bool operator<(const Data &b) const
    {
        if (x == b.x) {
            return y < b.y;
        }
        return x < b.x;
    }

    bool operator!=(const Data &b) const
    {
        return !(x == b.x && y == b.y);
    }
};

class Solution
{
private:
    int n, m;
    const int dx[4] = {0, 0, 1, -1};
    const int dy[4] = {1, -1, 0, 0};

    bool in(Data &data)
    {
        return (0 <= data.x && data.x < n) && (0 <= data.y && data.y < m);
    }

public:
    bool hasPath(vector<vector<int>> &maze, vector<int> &start,
                 vector<int> &destination)
    {
        // Use BFS
        // DFS will will have worse case that's really bad
        n = maze.size();
        if (n == 0)
            return false;
        m = maze[0].size();
        if (m == 0)
            return false;

        queue<Data> q;
        set<Data> seen;

        Data nxt = Data{start[0], start[1]};
        q.push(nxt);
        seen.insert(nxt);
        while (q.size() > 0) {
            Data &cur = q.front();
            q.pop();

            if (cur.x == destination[0] && cur.y == destination[1])
                return true;

            for (int i = 0; i < 4; i++) {
                nxt = Data{cur.x, cur.y};
                while (in(nxt) && maze[nxt.x][nxt.y] == 0) {
                    nxt.x += dx[i];
                    nxt.y += dy[i];
                }
                nxt.x -= dx[i];
                nxt.y -= dy[i];

                if (in(nxt) && nxt != cur && maze[nxt.x][nxt.y] == 0) {
                    if (seen.count(nxt) == 0) {
                        q.push(nxt);
                        seen.insert(nxt);
                    }
                }
            }
        }

        return false;
    }
};

#ifdef LOCAL
int main()
{
    return 0;
}
#endif