#include <iostream>
#include <utility>
#include <queue>
using namespace std;
#define INF 123456789
typedef pair<int,int> P;
int a, b, c, d, now, ans;
int af[100010],bf[100010],ae[100010],be[100010];
queue <P> q;
P x;
int main()
{
int i;
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>a>>b>>c>>d;
if(c==d&&d==0)
{
cout<<"0";
return 0;
}
if(c==0||d==0||c==a||d==b)
{
now=0;
}
else
{
cout<<"-1";
return 0;
}
for(i=0; i<=max(a,b); i++)
{
af[i]=bf[i]=ae[i]=be[i]=INF;
}
ae[0]=be[0]=0;
q.push({0,0});
while(!q.empty())
{
x=q.front();
q.pop();
now=INF;
if(x.first==0) now=min(now, ae[x.second]);
if(x.first==a) now=min(now, af[x.second]);
if(x.second==0) now=min(now, be[x.first]);
if(x.second==b) now=min(now, bf[x.first]);
if(x.first-(b-x.second)>=0)
{
if(bf[x.first-(b-x.second)]>now+1)
{
bf[x.first-(b-x.second)]=now+1;
q.push({x.first-(b-x.second),b});
}
}
else
{
if(ae[x.first+x.second]>now+1)
{
ae[x.first+x.second]=now+1;
q.push({0,x.first+x.second});
}
}
if(x.first+x.second>a) //a가 꽉참
{
if(af[x.second-(a-x.first)]>now+1)
{
af[x.second-(a-x.first)]=now+1;
q.push({a,x.second-(a-x.first)});
}
}
else
{
if(be[x.first+x.second]>now+1)
{
be[x.first+x.second]=now+1;
q.push({x.first+x.second, 0});
}
}
if(af[x.second]>now+1) //a에 꽉 채움
{
af[x.second]=now+1;
q.push({a,x.second});
}
if(ae[x.second]>now+1) //a를 비움
{
ae[x.second]=now+1;
q.push({0,x.second});
}
if(bf[x.first]>now+1) //b를 꽉 채움
{
bf[x.first]=now+1;
q.push({x.first, b});
}
if(be[x.first]>now+1)
{
be[x.first]=now+1;
q.push({x.first, 0});
}
}
ans=INF;
if(d==0) ans=min(ans, be[c]);
if(d==b) ans=min(ans, bf[c]);
if(c==0) ans=min(ans, ae[d]);
if(c==a) ans=min(ans, af[d]);
if(ans==INF)
{
cout<<"-1";
return 0;
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
2 ms |
472 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
4 ms |
1004 KB |
Output is correct |
8 |
Correct |
8 ms |
2156 KB |
Output is correct |
9 |
Correct |
8 ms |
2172 KB |
Output is correct |
10 |
Correct |
3 ms |
2172 KB |
Output is correct |
11 |
Correct |
2 ms |
2172 KB |
Output is correct |
12 |
Correct |
3 ms |
2172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
2 ms |
472 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
4 ms |
1004 KB |
Output is correct |
8 |
Correct |
8 ms |
2156 KB |
Output is correct |
9 |
Correct |
8 ms |
2172 KB |
Output is correct |
10 |
Correct |
3 ms |
2172 KB |
Output is correct |
11 |
Correct |
2 ms |
2172 KB |
Output is correct |
12 |
Correct |
2 ms |
2172 KB |
Output is correct |
13 |
Correct |
2 ms |
2172 KB |
Output is correct |
14 |
Correct |
2 ms |
2172 KB |
Output is correct |
15 |
Correct |
2 ms |
2172 KB |
Output is correct |
16 |
Correct |
2 ms |
2172 KB |
Output is correct |
17 |
Correct |
2 ms |
2172 KB |
Output is correct |
18 |
Correct |
4 ms |
2172 KB |
Output is correct |
19 |
Correct |
3 ms |
2172 KB |
Output is correct |
20 |
Correct |
2 ms |
2172 KB |
Output is correct |
21 |
Correct |
3 ms |
2172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2172 KB |
Output is correct |
2 |
Correct |
2 ms |
2172 KB |
Output is correct |
3 |
Correct |
3 ms |
2172 KB |
Output is correct |
4 |
Correct |
3 ms |
2172 KB |
Output is correct |
5 |
Correct |
2 ms |
2172 KB |
Output is correct |
6 |
Correct |
3 ms |
2172 KB |
Output is correct |
7 |
Correct |
2 ms |
2172 KB |
Output is correct |
8 |
Correct |
2 ms |
2172 KB |
Output is correct |
9 |
Correct |
3 ms |
2172 KB |
Output is correct |
10 |
Correct |
2 ms |
2172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
2 ms |
472 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
4 ms |
1004 KB |
Output is correct |
8 |
Correct |
8 ms |
2156 KB |
Output is correct |
9 |
Correct |
8 ms |
2172 KB |
Output is correct |
10 |
Correct |
3 ms |
2172 KB |
Output is correct |
11 |
Correct |
2 ms |
2172 KB |
Output is correct |
12 |
Correct |
2 ms |
2172 KB |
Output is correct |
13 |
Correct |
2 ms |
2172 KB |
Output is correct |
14 |
Correct |
2 ms |
2172 KB |
Output is correct |
15 |
Correct |
2 ms |
2172 KB |
Output is correct |
16 |
Correct |
2 ms |
2172 KB |
Output is correct |
17 |
Correct |
2 ms |
2172 KB |
Output is correct |
18 |
Correct |
4 ms |
2172 KB |
Output is correct |
19 |
Correct |
3 ms |
2172 KB |
Output is correct |
20 |
Correct |
2 ms |
2172 KB |
Output is correct |
21 |
Correct |
2 ms |
2172 KB |
Output is correct |
22 |
Correct |
2 ms |
2172 KB |
Output is correct |
23 |
Correct |
3 ms |
2172 KB |
Output is correct |
24 |
Correct |
3 ms |
2172 KB |
Output is correct |
25 |
Correct |
2 ms |
2172 KB |
Output is correct |
26 |
Correct |
3 ms |
2172 KB |
Output is correct |
27 |
Correct |
2 ms |
2172 KB |
Output is correct |
28 |
Correct |
2 ms |
2172 KB |
Output is correct |
29 |
Correct |
3 ms |
2172 KB |
Output is correct |
30 |
Correct |
2 ms |
2172 KB |
Output is correct |
31 |
Correct |
3 ms |
2172 KB |
Output is correct |
32 |
Correct |
3 ms |
2172 KB |
Output is correct |
33 |
Correct |
2 ms |
2172 KB |
Output is correct |
34 |
Correct |
3 ms |
2172 KB |
Output is correct |
35 |
Correct |
3 ms |
2172 KB |
Output is correct |
36 |
Correct |
3 ms |
2172 KB |
Output is correct |
37 |
Correct |
3 ms |
2172 KB |
Output is correct |
38 |
Correct |
2 ms |
2172 KB |
Output is correct |
39 |
Correct |
3 ms |
2172 KB |
Output is correct |
40 |
Correct |
3 ms |
2172 KB |
Output is correct |
41 |
Correct |
4 ms |
2172 KB |
Output is correct |
42 |
Correct |
5 ms |
2172 KB |
Output is correct |
43 |
Correct |
4 ms |
2172 KB |
Output is correct |
44 |
Correct |
4 ms |
2172 KB |
Output is correct |
45 |
Correct |
12 ms |
2172 KB |
Output is correct |
46 |
Correct |
3 ms |
2172 KB |
Output is correct |