zoj1005
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<int> vec;
int flag=0,ca,cb,n;
void find(int x,int y)
{
if(x==ca&&y==0){vec.push_back(-2);return;}
if(x==0&&y==cb){vec.push_back(-3);return;}
if(x!=ca&&y!=0&&flag!=-1)
{
vec.push_back(1);
flag=1;
int rem=y-ca+x;
if(rem>0)
find(ca,rem);
else
find(x+y,0);
}
else if(y!=cb&&x!=0&&flag!=1)
{
vec.push_back(-1);
flag=-1;
int rem=x-cb+y;
if(rem>0)
find(rem,cb);
else
find(0,x+y);
}
else if(x==0&&flag!=-2)
{
vec.push_back(2);
flag=2;
find(ca,y);
}
else if(x==ca&&flag!=2)
{
vec.push_back(-2);
flag=-2;
find(0,y);
}
else if(y==0&&flag!=-3)
{
vec.push_back(3);
flag=3;
find(x,cb);
}
else if(y==cb&&flag!=3)
{
vec.push_back(-3);
flag=-3;
find(x,0);
}
return;
}
void print(int f)
{
string str;
switch(f)
{
case 1:
str="pour A B";
break;
case -1:
str="pour B A";
break;
case -2:
str="fill A";
break;
case 2:
str="empty A";
break;
case -3:
str="fill B";
break;
case 3:
str="empty B";
}
cout<<str<<endl;
}
int main()
{
while(cin>>ca>>cb>>n)
{
find(0,n);
for(vector<int>::reverse_iterator riter=vec.rbegin();riter!=vec.rend();riter++)
if(--riter.base()!=vec.begin()||*riter!=2)print(*riter);
cout<<"success"<<endl;
vec.clear();
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
vector<int> vec;
int flag=0,ca,cb,n;
void find(int x,int y)
{
if(x==ca&&y==0){vec.push_back(-2);return;}
if(x==0&&y==cb){vec.push_back(-3);return;}
if(x!=ca&&y!=0&&flag!=-1)
{
vec.push_back(1);
flag=1;
int rem=y-ca+x;
if(rem>0)
find(ca,rem);
else
find(x+y,0);
}
else if(y!=cb&&x!=0&&flag!=1)
{
vec.push_back(-1);
flag=-1;
int rem=x-cb+y;
if(rem>0)
find(rem,cb);
else
find(0,x+y);
}
else if(x==0&&flag!=-2)
{
vec.push_back(2);
flag=2;
find(ca,y);
}
else if(x==ca&&flag!=2)
{
vec.push_back(-2);
flag=-2;
find(0,y);
}
else if(y==0&&flag!=-3)
{
vec.push_back(3);
flag=3;
find(x,cb);
}
else if(y==cb&&flag!=3)
{
vec.push_back(-3);
flag=-3;
find(x,0);
}
return;
}
void print(int f)
{
string str;
switch(f)
{
case 1:
str="pour A B";
break;
case -1:
str="pour B A";
break;
case -2:
str="fill A";
break;
case 2:
str="empty A";
break;
case -3:
str="fill B";
break;
case 3:
str="empty B";
}
cout<<str<<endl;
}
int main()
{
while(cin>>ca>>cb>>n)
{
find(0,n);
for(vector<int>::reverse_iterator riter=vec.rbegin();riter!=vec.rend();riter++)
if(--riter.base()!=vec.begin()||*riter!=2)print(*riter);
cout<<"success"<<endl;
vec.clear();
}
return 0;
}
终于给力了一把,第一次submit就AC了.
恩,继续继续~~