I - Pots -BFS

两个空瓶,三种操作

一、把一个瓶子灌满

二、把一个瓶子清空

三、把一个瓶子里面的水灌到另一个瓶子里面去


BFS暴力


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;

struct node
{
	
	int  me;
	int it;
	
	//	int c_vol;
	int ope;
	int pare;
	int step;
	
};
int vis[105][105];
int flag;
queue <node> qq;
int a,b,c;
node ans[1000000];
int ok=0;
void bfs()
{
	node start;
	start.me=0;
	start.it=0;
	start.ope=0;
	start.step=0;
	qq.push(start);
	while(!qq.empty())
	{
		
		node t=qq.front();
		qq.pop();
		if (vis[t.me][t.it]==1)
			continue;
		else
		{
			vis[t.me][t.it]=1;
		}
		if (t.me==c||t.it==c)
		{
			flag=t.step;  		
			return ;
		} 
		int i;
		for (i=1;i<=6;i++)
		{
			node k=t;
			if (i==1&&k.me!=0)
			{ 
				k.ope=1;   //me空
				k.me=0; 
			}else
			if(i==2&&k.it!=0)
			{ 
				k.ope=2;  // it空
				k.it=0; 
			}else
			if (i==3&&k.me<a)
			{ 
				k.ope=3;  //me满
				k.me=a;  
			}else
			if (i==4&&k.it<b)
			{ 
				k.ope=4;  //it满
				k.it=b;  
			}else
			if (i==5&&k.me!=0&&k.it!=b)
			{
				
				k.ope=5;  //me to it
				if (k.it+k.me>=b)
				{
					k.me-=(b-k.it);
						k.it=b;
				}
				else
				{
					k.it+=k.me;
					k.me=0;
				} 
			}else
			if (i==6&&k.it!=0&&k.me!=a)
			{
				
				k.ope=6;  //it to me
				if (k.it+k.me>=a)
				{
					
					k.it-=a-k.me;
					k.me=a;
				}
				else
				{
					k.me+=k.it;
					k.it=0;
				} 
				
				
			}else
				continue;;

			k.pare=t.step;
			k.step=ok+1; 
		//	if (vis[k.me][k.it]==1)
		//	continue;

			ans[++ok]=k; 
			qq.push(k);
		}
		
	}
	
	
	
}
int put (node tmp)
{
	if (tmp.ope==1)
		printf("DROP(1)\n");
	if (tmp.ope==2)
		printf("DROP(2)\n");
	if (tmp.ope==3)
		printf("FILL(1)\n");
	if (tmp.ope==4)
		printf("FILL(2)\n");
	if (tmp.ope==5)
		printf("POUR(1,2)\n");
	if (tmp.ope==6)
		printf("POUR(2,1)\n");
	
	return 0;
}
int cun=0;
int output(int stp)
{
	if (ans[stp].step==0)
	{
		
		printf("%d\n",cun);
		return 0;
	}
	else
	{
		cun++;
		output(  ans[stp].pare);
		put(ans[stp]);
	}
	
}


int main()
{
	
	cin>>a>>b>>c;
	flag=0;
	
			 
  memset(vis,0,sizeof(vis));

	bfs();
	if (!flag)
		printf("impossible\n");
	else
		
		output(flag );
	
	
	
	
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值