解題思路: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;
}
};