babadeleida
chl2023
2023-11-25 11:26:06
//AMAZE AMAZE HAZE
#include<bits/stdc++.h>
using namespace std;
int n,m,z,minn=INT_MAX;
int momo[1001][1001];
struct hazy{
int x,y;
};
queue<hazy> misty;
int main(){
cin>>n>>m>>z;
memset(momo,-1,sizeof(momo));
momo[0][0]=0;
misty.push({0,0});
while(!misty.empty()){
int x=misty.front().x,y=misty.front().y;
if(x+y==z){
if(momo[x][y]<=minn) minn=momo[x][y];
}
misty.pop();
if(momo[n][y]==-1){
momo[n][y]=momo[x][y]+1;
misty.push({n,y});
}
if(momo[0][y]==-1){
momo[0][y]=momo[x][y]+1;
misty.push({0,y});
}
if(momo[x][m]==-1){
momo[x][m]=momo[x][y]+1;
misty.push({x,m});
}
if(momo[x][0]==-1){
momo[x][0]=momo[x][y]+1;
misty.push({x,0});
}
int sum=min(x,m-y);
if(momo[x-sum][y+sum]==-1){
momo[x-sum][y+sum]=momo[x][y]+1;
misty.push({x-sum,y+sum});
}
sum=min(n-x,y);
if(momo[x+sum][y-sum]==-1){
momo[x+sum][y-sum]=momo[x][y]+1;
misty.push({x+sum,y-sum});
}
}
if(minn!=INT_MAX)cout<<minn;
else cout<<-1;
return 0;
}
共 1 条回复