您的位置:首页 > 其它

HDU 1495 非常可乐 bfs 难度:1

2015-03-14 20:27 561 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1495

第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int mxind=101;
const int maxn=mxind*mxind;
int n,m,s;
int dp[maxn];
queue<int>que;
void poursub(int &f,int &t,int ct){
int sub=ct-t;
if(f<sub){
t+=f;
f-=f;
}
else {
f-=sub;
t+=sub;
}
}
int pour(int fs,int fn,int fm,int op){
int ts=fs,tn=fn,tm=fm;
switch(op){
case 0:
poursub(ts,tn,n);
break;
case 1:
poursub(ts,tm,m);
break;
case 2:
poursub(tn,ts,s);
break;
case 3:
poursub(tn,tm,m);
break;
case 4:
poursub(tm,ts,s);
break;
case 5:
poursub(tm,tn,n);
break;
}
return ts*mxind+tn;
}
int main(){
while(scanf("%d%d%d",&s,&m,&n)==3&&s){
if((s&1)==1){
puts("NO");
continue;
}
memset(dp,0x7f,sizeof(dp));
while(!que.empty())que.pop();
int ss=s*mxind;
dp[ss]=0;
que.push(ss);
bool fl=false;
while(!que.empty()){
int f=que.front();que.pop();
int fs=f/mxind,fn=f%mxind,fm=s-fs-fn;
if(fs==s/2&&(fn==s/2||fm==s/2)){
printf("%d\n",dp[fs*mxind+fn]);
fl=true;
break;
}
for(int i=0;i<6;i++){
int tmp=pour(fs,fn,fm,i);
if(dp[tmp]>dp[f]+1){
dp[tmp]=dp[f]+1;
que.push(tmp);
}
}
}
if(!fl)puts("NO");
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: