洛谷P1432 倒水问题(CODEVS.1226)

To 洛谷.1432 倒水问题

题目背景

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

在电影“虎胆龙威3-纽约大劫案”中,布鲁斯·威利斯和杰里米·艾恩斯遇到这样一个难题:给他们一个3加仑水壶和一个5加仑水壶,要求在5加仑水壶里准确装入4加仑的水。真是个难题呢。

//恩可以不用在意这个,看看题目描述的翻译就行了。

题目描述

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A fill B empty A

empty B

pour A B

pour B A

success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

假定两个水壶A和B,供水量不限。可以使用三种方法装水:

给一个水壶装水;

把一个水壶倒空;

从一个水壶倒进另一个水壶。

当从一个水壶倒进另一个水壶时,如果第一个水壶倒空,或者第二个水壶装满就不能再倒了。例如,一个水壶A是5加仑和另一个水壶B是6加仑,水量是8加仑,则从水壶A倒进水壶B时,让水壶B充满水而水壶A剩3加仑水。

问题由3个参数:Ca,Cb和N,分别表示水壶A和B的容量,目标水量N。解决问题的目标是,给出一系列倒水的步骤,使水壶B中的水量恰好是N。

“pour A B”,表示将水从水壶A倒进水壶B;“success”表示目标已经完成。

我们假定每个输入都有一个解决方案。

//可能有多解,但是洛谷目前不支持spj,所以评测结果仅供参考。

输入输出格式

输入格式:

 

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

输入有多行,每行都是一个难题。每个难题有三个正整数:Ca,Cb和N。假设0<Ca≤Cb和N≤Cb≤1000,且A和B互质。

 

输出格式:

 

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

输出是由一系列倒水操作构成的,其目标是实现水壶B中有N加仑的水。最后一行是“success”;从第1列开始输出,行末没有空格。

 

输入输出样例

输入样例#1:
3 5 4 
5 7 3 
输出样例#1:
fill B 
pour B A 
empty A 
pour B A 
fill B 
pour B A 
success 
fill A 
pour A B 
fill A 
pour A B 
empty B 
pour A B 
success 

思路:

  广搜,注意状态即可。

代码:

1.洛谷.1432

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 const string oper[6]=
 6 {
 7     "fill A","fill B","empty A",
 8     "empty B","pour A B","pour B A"
 9 };
10 struct state
11 {
12     int x,y,step,pre,op;
13 }q[1024*1024];
14 int a,b,c,head,tail,ans[1024];
15 bool vis[1024][1024];
16 void add(int xn,int yn,int stepn,int pre,int op)
17 {
18     if(vis[xn][yn]) return;
19     vis[xn][yn]=1;
20     q[++tail].x=xn;
21     q[tail].y=yn;
22     q[tail].step=stepn+1;
23     q[tail].pre=pre;
24     q[tail].op=op;
25 }
26 void output(int x)
27 {
28     if(x==1) return;
29     output(q[x].pre);
30     cout<<oper[q[x].op]<<endl;
31 }
32 void print(int x)
33 {
34     //printf("%d\n",q[x].step);
35     output(x);
36     puts("success");
37 }
38 void bfs()
39 {
40     q[++head].x=0;
41     q[head].y=0;
42     q[head].step=0;
43     q[head].pre=1;
44     vis[0][0]=1;
45     while(head<=tail)
46     {
47         int xx=q[head].x;
48         int yy=q[head].y;
49         int st=q[head].step;
50         if(yy==c)
51         {
52             print(head);
53             return;
54         }
55         add(a,yy,st,head,0);
56         add(xx,b,st,head,1);
57         add(0,yy,st,head,2);
58         add(xx,0,st,head,3);
59         int tmp=min(xx,b-yy);
60         add(xx-tmp,yy+tmp,st,head,4);
61         tmp=min(a-xx,yy);
62         add(xx+tmp,yy-tmp,st,head,5);
63         head++;
64     }
65 }
66 int main()
67 {
68     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
69     {
70         memset(vis,0,sizeof(vis));
71         head=0;tail=1;
72         bfs();
73     }    
74     return 0;
75 }
洛谷

2.CODEVS.1226

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 struct node
 5 {
 6     int x,y,step;
 7 }q[300*300];
 8 int a,b,c,head,tail=1;
 9 bool vis[300][300];
10 void add(int xn,int yn,int stepn)
11 {
12     if(vis[xn][yn]) return;
13     vis[xn][yn]=1;
14     q[++tail].x=xn;
15     q[tail].y=yn;
16     q[tail].step=stepn+1;
17 }
18 void bfs()
19 {
20     q[++head].x=0;
21     q[head].y=0;
22     q[head].step=0;
23     vis[0][0]=1;
24     while(head<=tail)
25     {
26         int xx=q[head].x;
27         int yy=q[head].y;
28         int st=q[head].step;
29         if(xx==c||yy==c)
30         {
31             printf("%d",q[head].step);
32             return;
33         }
34         add(a,yy,st);
35         add(xx,b,st);
36         add(0,yy,st);
37         add(xx,0,st);
38         int tmp=min(xx,b-yy);
39         add(xx-tmp,yy+tmp,st);
40         tmp=min(a-xx,yy);
41         add(xx+tmp,yy-tmp,st);
42         head++;
43     }
44     printf("impossible");
45 }
46 int main()
47 {
48     scanf("%d%d%d",&a,&b,&c);
49     if(a==c||b==c)
50     {
51         printf("0");return 0;
52     }
53     bfs();
54     return 0;
55 }
CODEVS

 



转载于:https://www.cnblogs.com/SovietPower/p/6888987.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值