倒水问题<隐式图遍历><结构体中帮设置优先队列>

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;}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值