POJ 3414 Pots(bfs)

传送门:点击打开链接

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17576 Accepted: 7441 Special Judge

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题意是有两个杯子,容量已知,需要凑出题目要求的容量,输出总数加路径,若不存在输出impossible。操作有6种,倒满1、2;倒空1、2;1倒向2;2倒向1.


bfs枚举六种情况,一步一步向下搜。这里用数组模拟链表,因为当前操作后的状态就是下一步要做的操作,所以前后状态是有关系的。用数组标记两杯水的水量,若经过6种操作,出现的结果都被标记,说明已经进入循环,无法得到目标结果。即输出impossible。


代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;
const int maxn=105;

int x,y,z,r,l,step[maxn*maxn];
bool visit[maxn][maxn];
string name[7]={" ", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(2,1)", "POUR(1,2)"};//记录的编号从1到6,所以名字第一位定位空 
struct node{
	int x,y,count,pre;  //x第一杯水,y第二杯水,count记录进行的哪一步,pre记录前一步操作即用结构体数组模拟链表 
}p[maxn*maxn];

void solve(int a,int b,int c)
{
	if(visit[a][b])   //已经被标记,即第一杯水跟第二杯水的量前面已经出现 
	return ;
	
	visit[a][b]=true; //标记 
	p[r].x=a;    //存入下一个节点,即进行下一步操作 
	p[r].y=b;
	p[r].count=c;
	p[r].pre=l;
	r++;
}

void display()
{
	int i,ans=0;
	while(l!=0)  //未到达头节点 
	{ 
		step[ans++]=p[l].count; //下标自加,记录执行状态 
		l=p[l].pre;      //l后移 
	}
	cout<<ans<<endl; //总数 
	for(i=ans-1;i>=0;i--)
	{
		cout<<name[step[i]]<<endl;  //输出状态 
	}
}

void bfs()
{
	int tempx,tempy;  //记录x和y值的变化 
	p[0].x=p[0].y=0;  //头节点两个空杯子 
	visit[0][0]=true; //标记 
	l=0;   //头节点
	r=1;   //下一步的状态 
	
	while(l!=r) //“=”代表6种操作都出现以前出现过的状态,说明陷入循环,即不存在答案 
	{
		if(p[l].y==z||p[l].x==z) //一瓶水达到要求的值 
		{
			display();
			return ;
		}
		
		tempx=x;tempy=p[l].y;//倒满1 
		solve(tempx,tempy,1);
		
		tempx=p[l].x;tempy=y;//倒满2 
		solve(tempx,tempy,2);
		
		tempx=0;tempy=p[l].y;//倒空1 
		solve(tempx,tempy,3);
		
		tempx=p[l].x;tempy=0;//倒空2
		solve(tempx,tempy,4);
		
		tempx=p[l].x+min(x-p[l].x,p[l].y);//2倒向1 
		tempy=p[l].y-min(x-p[l].x,p[l].y);
		solve(tempx,tempy,5);
		
		tempx=p[l].x-min(y-p[l].y,p[l].x);//2倒向1 
		tempy=p[l].y+min(y-p[l].y,p[l].x);
		solve(tempx,tempy,6);
		
		l++;
	}
	cout<<"impossible"<<endl;
}

int main()
{
	while(cin>>x>>y>>z)
	{
		memset(visit,false,sizeof(visit)); //清空标记数组 
		bfs();
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信博6主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值