-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Moves to Reach Target with Rotations.cpp
82 lines (75 loc) · 2.05 KB
/
Minimum Moves to Reach Target with Rotations.cpp
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
// Runtime: 296 ms (Top 23.08%) | Memory: 45.5 MB (Top 20.19%)
class Solution {
public:
int n;
set<vector<int>> visited;
bool candown(vector<vector<int>>& grid,int x,int y,bool hor){
if(!hor){
if(x+2<n && grid[x+1][y]==0 && grid[x+2][y]==0 && !visited.count({x+1,y,hor})){
return true;
}
}else{
if(x+1<n && y+1<n && grid[x+1][y]==0 && grid[x+1][y+1]==0 && !visited.count({x+1,y,hor})){
return true;
}
}
return false;
}
bool canright(vector<vector<int>>& grid,int x,int y,bool hor){
if(hor){
if(y+2<n && grid[x][y+1]==0 && grid[x][y+2]==0 && !visited.count({x,y+1,hor})){
return true;
}
}else{
if(y+1<n && x+1<n && grid[x][y+1]==0 && grid[x+1][y+1]==0 && !visited.count({x,y+1,hor})){
return true;
}
}
return false;
}
bool canrot(vector<vector<int>>& grid,int x,int y,bool hor){
if(hor){
if(y+1<n && x+1<n && grid[x+1][y]==0 && grid[x+1][y+1]==0 && !visited.count({x,y,!hor})){
return true;
}
}else{
if(x+1<n && y+1<n && grid[x][y+1]==0 &&grid[x+1][y+1]==0 && !visited.count({x,y,!hor})){
return true;
}
}
return false;
}
int minimumMoves(vector<vector<int>>& grid) {
queue<array<int, 3>> q;
q.push({0,0,true});
int ans=0;
n=grid.size();
while(!q.empty()){
int size=q.size();
while(size--){
auto a=q.front();
q.pop();
if(visited.count({a[0],a[1],a[2]})){
continue;
}
visited.insert({a[0],a[1],a[2]});
int x=a[0];
int y=a[1];
if(x==n-1 && y==n-2 && a[2]==1){
return ans;
}
if(candown(grid,x,y,a[2])){
q.push({x+1,y,a[2]});
}
if(canrot(grid,x,y,a[2])){
q.push({x,y,!a[2]});
}
if(canright(grid,x,y,a[2])){
q.push({x,y+1,a[2]});
}
}
ans++;
}
return -1;
}
};