Article Directory
1. Title
Do you remember the greedy snake that swept the world?
We're in a n*n
building on the grid a new maze map, the length of the snake is 2, which means that it will take up two cells.
The snake will ((0, 0) 和 (0, 1))
start moving from the upper left corner .
We use 0 for empty cells and 1 for obstacles.
The snake needs to move to the lower right corner of the maze ((n-1, n-2) 和 (n-1, n-1))
.
Every time it moves, the snake can walk like this:
-
If there are no obstacles, move one cell to the right . And still maintain the horizontal/vertical state of the body.
-
If there are no obstacles, move down one cell. And still maintain the horizontal/vertical state of the body.
-
If it is horizontal and the two cells below it are empty , rotate it 90 degrees clockwise. Snake from
((r, c)、(r, c+1))移动到 ((r, c)、(r+1, c))
.
-
If it is upright and the two cells to its right are empty , rotate it 90 degrees counterclockwise. Snake from
((r, c)、(r+1, c))移动到((r, c)、(r, c+1))
.
Return the minimum number of moves the snake needs to reach its destination .
If you cannot reach the destination, return -1.
Example 1:
输入:grid = [[0,0,0,0,0,1],
[1,1,0,0,1,0],
[0,0,0,0,1,1],
[0,0,1,0,1,0],
[0,1,1,0,0,0],
[0,1,1,0,0,0]]
输出:11
解释:
一种可能的解决方案是
[右, 右, 顺时针旋转, 右, 下, 下, 下, 下, 逆时针旋转, 右, 下]。
示例 2:
输入:grid = [[0,0,1,1,1,1],
[0,0,0,0,1,1],
[1,1,0,0,0,1],
[1,1,1,0,0,1],
[1,1,1,0,0,1],
[1,1,1,0,0,0]]
输出:9
提示:
2 <= n <= 100
0 <= grid[i][j] <= 1
蛇保证从空单元格开始出发。
Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/minimum-moves-to-reach-target-with-rotations
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
2. Problem solving
- Encode the tail's coordinates x, y and direction d into 101 base numbers
- Then the snake can move in three ways: forward, overall translation , and rotation around the tail
class Solution {
vector<vector<int>> dir = {
{
0,1}, {
1,0}};
int n;
public:
int minimumMoves(vector<vector<int>>& grid) {
n = grid.size();
if(grid[n-1][n-2] || grid[n-1][n-1])
return -1;
int state, nt, pos, d, nd, i, j, k, x, y, x1, y1, x2, y2;
queue<int> q;
//将xy坐标压缩为一个数,再乘以 101,+ 方位,全部压缩为一个数
unordered_set<int> vis;
q.push(0);// pos = (101*i + j)*101 + dir
int step = 0, size;
int target = (101*(n-1)+(n-2))*101;
while(!q.empty())
{
size = q.size();
while(size--)
{
state = q.front();
q.pop();
if(state == target)
return step;
d = state%101;
pos = state/101;
i = pos/101;//原来尾巴位置
j = pos%101;
// cout << " i :" << i << " j: " << j << " d : " << d << endl;
x = i+dir[d][0];//原来头的位置
y = j+dir[d][1];
// 直行,方向不变
x1 = i+dir[d][0];//下一个尾巴占据的位置
y1 = j+dir[d][1];
x2 = x+ dir[d][0];//下一个头的位置
y2 = y+ dir[d][1];
nt = (101*x1+y1)*101+d;//下一个状态
if(ok(x2, y2) && grid[x2][y2]== 0
&& !vis.count(nt))
{
vis.insert(nt);
q.push(nt);//下一个状态
}
// 平移,方向不变
nd = d == 0 ? 1 : 0;
x1 = i+dir[nd][0];//下一个尾巴占据的位置
y1 = j+dir[nd][1];
x2 = x+ dir[nd][0];//下一个头的位置
y2 = y+ dir[nd][1];
nt = (101*x1+y1)*101+d;//下一个状态
if(ok(x1, y1) && grid[x1][y1]==0
&& ok(x2, y2) && grid[x2][y2]== 0
&& !vis.count(nt))
{
vis.insert(nt);
q.push(nt);
}
// 旋转,方向变化, 尾巴位置没变
nt = state/101*101 + nd;//下一个位置的编码
if(ok(x1, y1) && grid[x1][y1]==0
&& ok(x2, y2) && grid[x2][y2]== 0
&& !vis.count(nt))
{
vis.insert(nt);
q.push(nt);
}
}
step++;
}
return -1;
}
bool ok(int x, int y)
{
return x>=0 && x < n && y>=0 && y<n;
}
};
132 ms 17.5 MB
My CSDN blog address https://michael.blog.csdn.net/
Long press or scan the code to follow my official account (Michael Amin), cheer together, learn and progress together!