-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12761.cpp
More file actions
55 lines (54 loc) · 1.11 KB
/
12761.cpp
File metadata and controls
55 lines (54 loc) · 1.11 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int diff[6]={1,-1,0};
int main()
{
int a,b,n,m;
cin>>a>>b>>n>>m;
diff[2]=a;
diff[3]=-a;
diff[4]=b;
diff[5]=-b;
vector<int> v;
v.assign(100001,987654321);
queue<int> q;
q.push(n);
v[n]=0;
while(!q.empty()){
int cur=q.front();
q.pop();
for(int k=0;k<6;k++)
{
int next=cur+diff[k];
if(next>=0&&next<=100000&&v[next]>v[cur]+1)
{
q.push(next);
v[next]=v[cur]+1;
}
if(next==m)
{
cout<<v[next];
return 0;
}
}
for(int i=1;i<=2;i++)
{
int next=cur*diff[2*i];
if(next>=0&&next<=100000&&v[next]>v[cur]+1)
{
q.push(next);
v[next]=v[cur]+1;
}
if(next==m)
{
cout<<v[next];
return 0;
}
}
}
cout<<v[m]<<endl;
return 0;
}