-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path161. Minimum Opertation to get Goal.cpp
More file actions
34 lines (30 loc) · 1.06 KB
/
Copy path161. Minimum Opertation to get Goal.cpp
File metadata and controls
34 lines (30 loc) · 1.06 KB
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
// If 0 <= x <= 1000, then for any index i in the array (0 <= i < nums.length), you can set x
// to any of the following:
//
// x + nums[i]
// x - nums[i]
// x ^ nums[i] (bitwise-XOR)
int minimumOperations(vector<int>& nums, int start, int goal) {
vector<bool>visited(1001,false);
int ans=0;
queue<int>q;
q.push(start);
while(!q.empty()) {
int size=q.size();
while(size--) {
int node=q.front(); q.pop();
if(node==goal)
return ans;
if(node>1000 || node<0 || visited[node])
continue;
visited[node]=true;
for(int i=0; i<nums.size(); i++) {
int a=node+nums[i],b=node-nums[i],c=node^nums[i];
for(auto j :{a,b,c})
q.push(j);
}
}
ans++;
}
return -1;
}