广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13547   Accepted: 5718   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)
  1 /*和之前的那道倒水题目十分相似,唯一的难点就是输出倒水的方式,我们可以记录每一个搜到的状态的前一状态在队列中的编号和,该状态与前一状态的关系,再递归输出结果即可*/
  2 #include<iostream>
  3 using namespace std;
  4 #include<cstdio>
  5 #include<cstring>
  6 #define N 10010
  7 int head=-1,tail=-1;
  8 int a,b,c;
  9 bool flag[101][101]={0};
 10 struct node{
 11     int a1,b1,pre,dis,relat;
 12 }que[N];
 13 int bfs()
 14 {
 15     while(head<tail)
 16     {
 17         ++head;
 18         if(que[head].a1==c||que[head].b1==c)
 19         {
 20             return head;
 21         }
 22         if(!flag[que[head].a1][0])
 23         {
 24             flag[que[head].a1][0]=true;
 25             node nex;
 26             nex.a1=que[head].a1;nex.b1=0;
 27             nex.dis=que[head].dis+1;
 28             nex.pre=head;
 29             nex.relat=4;
 30             ++tail;
 31             que[tail]=nex;
 32         }
 33         if(!flag[0][que[head].b1])
 34         {
 35             flag[0][que[head].b1]=true;
 36             node nex;
 37             nex.a1=0;nex.b1=que[head].b1;
 38             nex.dis=que[head].dis+1;
 39             nex.pre=head;
 40             nex.relat=3;
 41             ++tail;
 42             que[tail]=nex;
 43         }
 44         if(!flag[que[head].a1][b])
 45         {
 46             flag[que[head].a1][b]=true;
 47             node nex;
 48             nex.a1=que[head].a1;nex.b1=b;
 49             nex.dis=que[head].dis+1;
 50             nex.pre=head;
 51             nex.relat=2;
 52             ++tail;
 53             que[tail]=nex;
 54         }
 55         if(!flag[a][que[head].b1])
 56         {
 57             flag[a][que[head].b1]=true;
 58             node nex;
 59             nex.a1=a;nex.b1=que[head].b1;
 60             nex.dis=que[head].dis+1;
 61             nex.pre=head;
 62             nex.relat=1;
 63             ++tail;
 64             que[tail]=nex;
 65         }
 66         if(que[head].a1>=(b-que[head].b1)&&!flag[que[head].a1-(b-que[head].b1)][b])
 67         {
 68             flag[que[head].a1-(b-que[head].b1)][b]=true;
 69             node nex;
 70             nex.a1=que[head].a1-(b-que[head].b1);nex.b1=b;
 71             nex.dis=que[head].dis+1;
 72             nex.pre=head;
 73             nex.relat=5;
 74             ++tail;
 75             que[tail]=nex;
 76         }
 77         if(que[head].a1<(b-que[head].b1)&&!flag[0][que[head].a1+que[head].b1])
 78         {
 79             flag[0][que[head].a1+que[head].b1]=true;
 80             node nex;
 81             nex.a1=0;nex.b1=que[head].a1+que[head].b1;
 82             nex.dis=que[head].dis+1;
 83             nex.pre=head;
 84             nex.relat=5;
 85             ++tail;
 86             que[tail]=nex;
 87         }
 88         if(que[head].b1>=(a-que[head].a1)&&!flag[a][que[head].b1-(a-que[head].a1)])
 89         {
 90             flag[a][que[head].b1-(a-que[head].a1)]=true;
 91             node nex;
 92             nex.a1=a;nex.b1=que[head].b1-(a-que[head].a1);
 93             nex.dis=que[head].dis+1;
 94             nex.pre=head;
 95             nex.relat=6;
 96             ++tail;
 97             que[tail]=nex;
 98         }
 99         if(que[head].b1<(a-que[head].a1)&&!flag[que[head].a1+que[head].b1][0])
100         {
101             flag[que[head].a1+que[head].b1][0]=true;
102             node nex;
103             nex.a1=que[head].a1+que[head].b1;nex.b1=0;
104             nex.dis=que[head].dis+1;
105             nex.pre=head;
106             nex.relat=6;
107             ++tail;
108             que[tail]=nex;
109         }
110     }
111     return -1;
112 }
113 void out(int temp)
114 {
115     if(que[que[temp].pre].pre!=-1)
116       out(que[temp].pre);
117     if(que[temp].relat==1)
118       printf("FILL(1)\n");
119     else if(que[temp].relat==2)
120            printf("FILL(2)\n");
121          else if(que[temp].relat==3)
122                 printf("DROP(1)\n");
123               else if(que[temp].relat==4)
124                      printf("DROP(2)\n");
125                    else if(que[temp].relat==5)
126                           printf("POUR(1,2)\n");
127                         else printf("POUR(2,1)\n");
128 }
129 /*
130 FILL(2)
131 POUR(2,1)
132 DROP(1)
133 POUR(2,1)
134 FILL(2)
135 POUR(2,1)
136 */
137 int main()
138 {
139     scanf("%d%d%d",&a,&b,&c);
140     ++tail;
141     que[tail].a1=0;que[tail].b1=0;
142     que[tail].dis=0;que[tail].pre=-1;
143     flag[0][0]=true;
144     int temp=bfs();
145     if(temp==-1)
146      printf("impossible\n");
147     else {
148         printf("%d\n",que[head].dis);
149         out(temp);
150     }
151     return 0;
152 }
时间: 2024-08-05 11:15:11

广搜+输出路径 POJ 3414 Pots的相关文章

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 3414 Pots (经典bfs )

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

poj 3414 Pots【bfs+回溯路径 正向输出】

题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12080   Accepted: 5104   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operation

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

广搜+打表 POJ 1426 Find The Multiple

POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Accepted: 10613   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representati

poj 3414 Pots (bfs+线索)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 3414 Pots 记录路径的广搜

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill th

POJ - 3414 Pots (BFS+路径记录)

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7