[leetcode]5366. 檢查網格中是否存在有效路徑

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

解題思路:BFS

在這裏插入圖片描述

AC的代碼:

struct Point
{
    int x,y;
    int d;
    Point(int _x, int _y, int _d)
    {
        x = _x, y = _y, d = _d;
    }
};
class Solution {
    bool isOk(int cur, int next, int dir)
    {
        if(dir == 0 && (cur == 2 || cur == 5 || cur == 6) && (next == 2 || next == 3 || next == 4))
        {
            return true;
        }
        else if(dir == 1 && (cur == 1 || cur == 4 || cur == 6) && (next == 1 || next == 3 || next == 5))
        {
            return true;
        }
        else if(dir == 2 && (cur == 2 || cur == 3 || cur == 4) && (next == 2 || next == 5 || next == 6))
        {
            return true;
        }
        else if(dir == 3 && (cur == 1 || cur == 3 || cur == 5) && (next == 1 || next == 4 || next == 6))
        {
            return true;
        }
        return false;
    }
public:
    bool hasValidPath(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<bool>>visited(m, vector<bool>(n,false));
        queue<Point>q;
        q.push(Point(0,0,grid[0][0]));
        int dx[4] = {-1, 0, 1, 0};
        int dy[4] = {0, 1, 0, -1};
        while(!q.empty())
        {
            Point cur = q.front(); q.pop();
            visited[cur.x][cur.y] = true;
            if(cur.x == m-1 && cur.y == n-1)
            {
                return true;
            }
            for(int i = 0; i < 4; i++)
            {
                int nextX = cur.x + dx[i], nextY = cur.y + dy[i];
                if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visited[nextX][nextY]) continue;
                
                if(isOk(cur.d, grid[nextX][nextY], i))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
            }
        }
        return false;
    }
};




沒通過的代碼:

struct Point
{
    int x,y;
    int d;
    Point(int _x, int _y, int _d)
    {
        x = _x, y = _y, d = _d;
    }
};
class Solution {
public:
    bool hasValidPath(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<bool>>visited(m, vector<bool>(n,false));
        queue<Point>q;
        q.push(Point(0,0,grid[0][0]));
        int dx[4] = {-1, 0, 1, 0};
        int dy[4] = {0, 1, 0, -1};
        while(!q.empty())
        {
            Point cur = q.front(); q.pop();
            visited[cur.x][cur.y] = true;
            if(cur.x == m-1 && cur.y == n-1)
            {
                return true;
            }
            for(int i = 0; i < 4; i++)
            {
                int nextX = cur.x + dx[i], nextY = cur.y + dy[i];
                if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visited[nextX][nextY]) continue;
                
                if(i == 1 && cur.d == 1 && ( grid[nextX][nextY] == 1 || grid[nextX][nextY] == 3 ||  grid[nextX][nextY] == 5))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 3 && cur.d == 1 && (grid[nextX][nextY] == 1 ||grid[nextX][nextY] == 4 || grid[nextX][nextY] == 6))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 2 && cur.d == 2 && grid[nextX][nextY] != 1)
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 0 && cur.d == 2 && (grid[nextX][nextY] == 3 || grid[nextX][nextY] == 4 || grid[nextX][nextY] == 2))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 2 && cur.d == 3 && grid[nextX][nextY] != 3 && grid[nextX][nextY] != 1 && grid[nextX][nextY] != 4)
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 3 && cur.d == 3 && (grid[nextX][nextY] == 6 || grid[nextX][nextY] == 4 || grid[nextX][nextY] == 1))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 2 &&cur.d == 4 && (grid[nextX][nextY] == 2 || grid[nextX][nextY] == 5 || grid[nextX][nextY] == 6))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 1 &&cur.d == 4 && (grid[nextX][nextY] == 3 || grid[nextX][nextY] == 5 || grid[nextX][nextY] == 1))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 0 && cur.d == 5 && grid[nextX][nextY] != 5 && grid[nextX][nextY] != 1 && grid[nextX][nextY] != 6)
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 2 && cur.d == 5 && (grid[nextX][nextY] == 1 || grid[nextX][nextY] == 6))
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 0 && cur.d == 6 &&  grid[nextX][nextY] != 1 && grid[nextX][nextY] != 6 && grid[nextX][nextY] != 5)
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
                if(i == 1 && cur.d == 6 &&  grid[nextX][nextY] != 2 && grid[nextX][nextY] != 6 && grid[nextX][nextY] != 4)
                {
                    q.push(Point(nextX, nextY, grid[nextX][nextY]));
                }
            }
        }
        return false;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章