ACM | Assignments 6
7-2 非常可乐
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
输入格式:
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
输出格式:
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
输入样例:
7 4 3
4 1 3
0 0 0
输出样例:
NO
3
Code
WA代码
#include<bits/stdc++.h>
using namespace std;
struct State{
int s,a,b;
int step;
};
bool used[105][105][105];
int bfs(int s,int a,int b){
if(s%2==1)
return -1;
queue<State> q;
State now;
now.s=s;
now.a=0;
now.b=0;
now.step=0;
q.push(now);
while(!q.empty()){
now=q.front();
q.pop();
cout<<now.s<<" "<<now.a<<" "<<now.b<<" "<<now.step<<endl;
// if(used[now.s][now.a][now.b])
// continue;
// if(now.a==s/2 || now.b==s/2 || now.s==s/2)
if((now.a==s/2 && now.b==s/2)||(now.a==s/2 && now.s==s/2)||(now.s==s/2 && now.b==s/2))
return now.step;
used[now.s][now.a][now.b]=true;
State newstate;
newstate.step=now.step+1;
//S->A
if(now.s>=a-now.a){
newstate.s=now.s-(a-now.a);
newstate.a=now.a+(a-now.a);
newstate.b=now.b;
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}
//S->B
if(now.s>=b-now.b){
newstate.s=now.s-(b-now.b);
newstate.a=now.a;
newstate.b=now.b+(b-now.b);
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}
//A->S
// if(now.a<s-now.s)
{
newstate.s=now.s+now.a;
newstate.a=now.a-now.a;
newstate.b=now.b;
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}
//A->B
if(now.a<b-now.b){
newstate.s=now.s;
newstate.a=now.a-now.a;
newstate.b=now.b+now.a;
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}else{
newstate.s=now.s;
newstate.a=now.a-(b-now.b);
newstate.b=now.b+(b-now.b);
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}
//B->S
// if(now.b<s-now.s)
{
newstate.s=now.s+now.b;
newstate.a=now.a;
newstate.b=now.b-now.b;
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}
//B->A
if(now.b<a-now.a){
newstate.s=now.s;
newstate.a=now.a+now.b;
newstate.b=now.b-now.b;
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}else{
newstate.s=now.s;
newstate.a=now.a+(a-now.a);
newstate.b=now.b-(a-now.a);
if(!used[newstate.s][newstate.a][newstate.b])
q.push(newstate);
}
}
return -1;
}
int main(){
int s,n,m;
while(cin>>s>>n>>m && s!=0){
memset(used,false,sizeof(used));
int t=bfs(s,n,m);
if(t!=-1){
cout<<t<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}
Code2
AC代码
#include<bits/stdc++.h>
using namespace std;
struct State{
int v[3];
int step;
};
bool used[105][105][105];
State pour(State s,int from,int to,int v[]){
State res=s;
res.step++;
int temp=res.v[from]+res.v[to];
if(temp>v[to]){
res.v[from]=temp-v[to];
res.v[to]=v[to];
}else{
res.v[to]=temp;
res.v[from]=0;˜
}
return res;
}
int bfs(int s,int a,int b){
if(s%2==1)
return -1;
queue<State> q;
State now;
int from[]={0,0,1,1,2,2};
int to[]={1,2,0,2,0,1};
int v[3]={s,a,b};
now.v[0]=s;
now.v[1]=0;
now.v[2]=0;
now.step=0;
q.push(now);
State newstate;
while(!q.empty()){
now=q.front();
q.pop();
// cout<<now.v[0]<<" "<<now.v[1]<<" "<<now.v[2]<<" "<<now.step<<endl;
if((now.v[0]==s/2 && now.v[1]==s/2)||(now.v[0]==s/2 && now.v[2]==s/2)||(now.v[1]==s/2 && now.v[2]==s/2))
return now.step;
used[now.v[0]][now.v[1]][now.v[2]]=true;
for(int i=0;i<6;i++){
newstate=pour(now,from[i],to[i],v);
if(!used[newstate.v[0]][newstate.v[1]][newstate.v[2]])
q.push(newstate);
}
}
return -1;
}
int main(){
int s,n,m;
while(cin>>s>>n>>m && s!=0){
memset(used,false,sizeof(used));
int t=bfs(s,n,m);
if(t!=-1) cout<<t<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
ACM | Assignments 6
https://acm.nanyan.cc/posts/42c5.html