kuangbin专题一H POJ3414 Pots(两种方法)

https://vjudge.net/contest/65959#problem/H

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 ≤ i ≤ 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 A, B, 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’.

 

这道题的难点主要在于记录路径,倒水问题还没太懂的,可以先看我的另一篇纯倒水的博客,会更好理解

https://blog.csdn.net/Double__E/article/details/87928167

【题目大意】

给出两个杯子的容量a,b(初始时两个杯子都为空)和目标水量c。在两个杯子之间进行不限次数的六种操作,分别是把a倒满,把b倒满,把a倒空,把b倒空,把a倒给b,把b倒给a(把a倒满,剩下的b留着,上同)。求最小a,b之间操作次数达到水量c。

这是一道广搜题,难点在于记录路径,有两种记录路径的方法,分别是在结构体里开数组(思路1),和开结构体数组(思路2)

【思路1】

1.把上述的六种操作分别标号1-6。

2.广搜传的是两个杯子当前的水量,取出队首元素,执行六种操作,依次入队,再取队首,再执行六种操作,再入队。。。。。直到任意一个杯子达到目标水量为止。

3.开一个结构体,里面存两个杯子当前的水量,步数和一个数组,这个数组里面记录着每一步执行的操作,每次执行下一步操作时,都要把上一步结构体中的数组复制给当前这一步结构体中的数组(注意:复制数组可以用函数memcpy),当前这一步结构体中的数组最后还要加上这一步执行的操作的编号,因为是广搜,所以找到的肯定是最短路程。

4.当任意一个杯子达到目标水量时,读取该结构体中的数组,把它”翻译“行的操作。

【思路1代码】

#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
int a1,b1,c,vis[101][101];
struct zb{
	int a,b;  //两个杯子当前的水量
	int step;  //步数
	int rec[100];  //执行过的操作编号
};
queue<zb>q;
void bfs(int aa,int bb){
	zb now,nex;
	now.a=aa;
	now.b=bb;
	now.step=0;
	memset(now.rec,0,sizeof(now.rec));
	vis[now.a][now.b]=1;
	q.push(now);
	while(!q.empty()){
		now=q.front();
		q.pop();
		if(now.a==c||now.b==c){  //其中一个到达目标水量时,读取到该结构体需要进行的操作
			printf("%d\n",now.step);
			for(int i=0;i<now.step;i++){ 
				if(now.rec[i]==1) printf("FILL(1)\n");
				if(now.rec[i]==2) printf("FILL(2)\n");
				if(now.rec[i]==3) printf("DROP(1)\n");
				if(now.rec[i]==4) printf("DROP(2)\n");
				if(now.rec[i]==5) printf("POUR(1,2)\n");
				if(now.rec[i]==6) printf("POUR(2,1)\n");
			}
			return;
		}
		int j;
		for(j=1;j<=6;j++){
			switch(j){  //分别执行六种操作 用1-6依次代表六种操作 
				case 1:{
					nex.a=a1;nex.b=now.b;
					break;
				}	
				case 2:{
					nex.b=b1;nex.a=now.a;
					break;
				}
				case 3:{
					nex.a=0;nex.b=now.b;
					break;
				}
				case 4:{
					nex.b=0;nex.a=now.a;
					break;
				}
				case 5:{
					if(now.a+now.b<=b1){
						nex.a=0;
						nex.b=now.b+now.a;
					}else{
						nex.b=b1;
						nex.a=now.a-(b1-now.b);
					}
					break;
				}
				case 6:{
					if(now.b+now.a<=a1){
						nex.b=0;
						nex.a=now.a+now.b;
					}else{
						nex.a=a1;
						nex.b=now.b-(a1-now.a);
					}
					break;
				}	
			}
			if(vis[nex.a][nex.b]) continue;
			vis[nex.a][nex.b]=1;
			nex.step=now.step+1;  //步数加一
			memcpy(nex.rec,now.rec,sizeof(int)*now.step);  //把上一步的操作复制过来
			nex.rec[now.step]=j;  //加上新操作
			q.push(nex);  //入队
		}
	}
	printf("impossible\n");
}
int main(){
	scanf("%d%d%d",&a1,&b1,&c);
	bfs(0,0);
	return 0;
}

【思路2】

1.思路2简单来说就是开一个结构体数组,结构体里存他的父亲(上一步)是哪个,每个结构体都有编号(这不是废话嘛),就这样。

结构体长这样:

struct xx{
 int pa,pb;//两个杯子当前的水量 
 int m;    //操作
 int t;    //步数
 int pre;  //上一步的结构体编号 
 int k;    //结构体编号 
}S[100000];

2.其他的地方跟【思路1】类似

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int a,b,c;
struct xx{
 int pa,pb;//两个杯子当前的水量 
 int m;    //操作
 int t;    //步数
 int pre;  //上一步的结构体编号 
 int k;    //结构体编号 
}S[100000];  //结构体数组 
int dis[300][300];
queue<xx>Q;
int k;
int bfs(){
	memset(dis,0,sizeof(dis));
	S[0].pa=0,S[0].pb=0,S[0].t=0,S[0].pre=0,S[0].m=-1,S[0].k=0;
	dis[0][0]=1;
	Q.push(S[0]);
	k++;
   	while(!Q.empty()){
			xx X=Q.front();
			if(X.pa==c||X.pb==c){
                printf("%d\n",X.t);
                return X.k;
			}
			Q.pop();
			for(int i=0;i<6;i++){
                int ta,tb;
                if(i==0&&X.pa<a) {ta=a,tb=X.pb;}//给a倒水
                if(i==1&&X.pb<b) {ta=X.pa,tb=b;}//给b倒水
                if(i==2&&X.pa>0) {
                    if(X.pa+X.pb>b) {
                        ta=X.pa-(b-X.pb),tb=b;
                    }
                    else {
                        ta=0,tb=X.pa+X.pb;
                    }
                }//a-->b
                if(i==3&&X.pb>0) {
                    if(X.pa+X.pb>a) ta=a,tb=X.pb-(a-X.pa);
                    else ta=X.pa+X.pb,tb=0;
                }//b-->a
                if(i==4&&X.pa>0) {
                    ta=0,tb=X.pb;
                }//把a倒掉
                if(i==5&&X.pb>0) {
                    ta=X.pa,tb=0;
                }//把b倒掉
                S[k].pa=ta,S[k].pb=tb;
				S[k].t=X.t+1;
				S[k].pre=X.k;
				S[k].m=i;
				S[k].k=k;
				if(S[k].pa<0||S[k].pa>a) continue;
				if(S[k].pb<0||S[k].pb>b) continue;
				if(!dis[ta][tb]){
					Q.push(S[k]);
					k++;
					dis[ta][tb]=1;
				}
			}
		}
    printf("impossible\n");
    return -1;
}
void f(int j){
 	if (S[j].k!=0) {
        f(S[j].pre);
        if(S[j].m==0) printf("FILL(1)\n");
        if(S[j].m==1) printf("FILL(2)\n");
        if(S[j].m==2) printf("POUR(1,2)\n");
        if(S[j].m==3) printf("POUR(2,1)\n");
        if(S[j].m==4) printf("DROP(1)\n");
        if(S[j].m==5) printf("DROP(2)\n");
    }
}
int main (){
    while(~scanf("%d %d %d",&a,&b,&c)){
       int ans=bfs();
       if(ans!=-1)f(ans);
       while(!Q.empty()) Q.pop();
    }
    return 0;
}

两种思路各有利弊,思路1费时间省空间,思路2费空间省时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值