题目:[kuangbin带你飞]专题一 简单搜索 M - 非常可乐
思路
- bfs没什么特别的,注意目标条件,奇数肯定不能平分。
- 输入顺序搞错了,wa了。
- 忘记重置了 wa了一次
- 搜索过程写的有点麻烦,用数组记录3个容器,然后再开一个对应编号的数组储存最大容量,代码量会少很多,但是懒得写了
AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 110;
int A,B,S;
bool vis[MAXN][MAXN][MAXN];
struct node{
int abot;
int bbot;
int sbot;
int steps;
};
node pour(node nownode,int i){
node nextnode;
nextnode = nownode;
switch(i){
case 0://a倒b
if(nownode.abot+nownode.bbot <=B){
nextnode.abot = 0;
nextnode.bbot = nownode.abot + nownode.bbot;
}else{
nextnode.abot = nownode.abot + nownode.bbot -B;
nextnode.bbot = B;
}
break;
case 1://b倒a
if(nownode.abot+nownode.bbot <=A){
nextnode.bbot = 0;
nextnode.abot = nownode.abot + nownode.bbot;
}else{
nextnode.bbot = nownode.abot + nownode.bbot -A;
nextnode.abot = A;
}
break;
case 2://a倒s
if(nownode.abot+nownode.sbot <=S){
nextnode.abot = 0;
nextnode.sbot = nownode.abot + nownode.sbot;
}else{
nextnode.abot = nownode.abot + nownode.sbot -S;
nextnode.sbot = S;
}
break;
case 3://b到s
if(nownode.abot+nownode.bbot <=S){
nextnode.bbot = 0;
nextnode.sbot = nownode.sbot + nownode.bbot;
}else{
nextnode.bbot = nownode.sbot + nownode.bbot -S;
nextnode.sbot = S;
}
break;
case 4://s倒b
if(nownode.sbot+nownode.bbot <=B){
nextnode.sbot = 0;
nextnode.bbot = nownode.sbot + nownode.bbot;
}else{
nextnode.sbot = nownode.sbot + nownode.bbot -B;
nextnode.bbot = B;
}
break;
case 5://s倒a
if(nownode.abot+nownode.sbot <=A){
nextnode.sbot = 0;
nextnode.abot = nownode.abot + nownode.sbot;
}else{
nextnode.sbot = nownode.abot + nownode.sbot -A;
nextnode.abot = A;
}
break;
}
return nextnode;
}
bool test(node nownode){
if(nownode.abot==S/2)
if(nownode.bbot == S/2)
return true;
else if(nownode.sbot == S/2)
return true;
if(nownode.bbot == S/2)
if(nownode.bbot == nownode.sbot)
return true;
return false;
}
int bfs(node st){
queue<node> Q;
Q.push(st);
vis[st.abot][st.bbot][st.sbot] = true;
while(!Q.empty()){
node nownode;
node nextnode;
nownode = Q.front();Q.pop();
if(test(nownode)){
// cout<<nownode.abot<<" "<<nownode.bbot<<" "<<nownode.sbot<<endl;
return nownode.steps;
}
// cout<<"-------------"<<endl;
for(int i= 0 ;i<6;i++){
nextnode = pour(nownode,i);
if(!vis[nextnode.abot][nextnode.bbot][nextnode.sbot]){
vis[nextnode.abot][nextnode.bbot][nextnode.sbot] = true;
nextnode.steps = nownode.steps +1;
// cout<<nownode.sbot<<" "<<nownode.abot<<" "<<nownode.bbot<<" "<<endl;
// cout<<"case"<<i<<endl;
Q.push(nextnode);
// cout<<nextnode.sbot<<" "<<nextnode.abot<<" "<<nextnode.bbot<<" "<<endl;
// cout<<"======="<<endl;
}
}
}
return -1;
}
int main(){
while(scanf("%d%d%d",&S,&A,&B)!=EOF&&A!=0&&B!=0&&S!=0){
memset(vis,false,sizeof(vis));
node st;
st.abot =0;
st.bbot =0;
st.sbot =S;
st.steps =0;
int ans=-1;
if(S%2==0)
ans = bfs(st);
if(ans<0) printf("NO\n");
else printf("%d\n",ans);
}
return 0;
}