the reason of failure:1、ttt新定义的结构体需要自己赋值,其初始值并不为0.
2、如果用普通队列存在t[1]便退出,得到的并不是最少倒水数。
3、状态的转移一定要以从队列出来的状态为基准而改变。
题意
样例输入
1 3 6 4
1 12 15 7
输出
4
14
代码:
#include <queue>
#include <string.h>
#include <iostream>
using namespace std;
struct ttt{
int a[3],step;
bool operator < (const ttt &b)const{
return step>b.step;
}
};
bool map[100][100];
int t[100];
void update(ttt t2){
for(int i=0;i<3;i++){
int d=t2.a[i];
if(t[d]<0||t2.step<t[d]){
t[d]=t2.step;
cout << "水为" << d << "进去了,步数为" << t2.step << endl;
}
}
}
int cup[3];
int main(){
freopen("in.txt","r",stdin);
int i,j,k,l;
int t1,t2,t3,t4;
priority_queue<ttt>qq;
memset(t,-1,sizeof(t));
cin >> cup[0] >> cup[1] >> cup[2] >> t1;
ttt s1,s2;
s1.a[2]=cup[2];s1.a[1]=0;s1.a[0]=0;
s1.step=0;
qq.push(s1);
while(!qq.empty()){
s1=qq.top();qq.pop();
update(s1);
//if(t[t1]>=0)break;//意思是存在到这个水位的方法
for(i=0;i<3;i++)
for(j=0;j<3;j++){
if(i!=j){
if(s1.a[i]==0||s1.a[j]==cup[j])continue;
int const1=min(cup[j],s1.a[i]+s1.a[j])-s1.a[j]; //再次表明状态的该变量应该用另外的值储存
s2=s1;
s2.step+=const1;
s2.a[j]=s1.a[j]+const1; //const1为倒的水量。
s2.a[i]=s1.a[i]-const1;
if(map[s2.a[i]][s2.a[j]]==0){
map[s2.a[i]][s2.a[j]]=1;
qq.push(s2);
}
}
}
}
for(int i=t1;i>=0;i--)
if(t[i]){
cout << t[i] << endl;
break;}
}