This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |