18.06.27 POJ 3414:Pots

描述

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.

输入

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).

输出

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’.

样例输入

3 5 4

样例输出

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
  1 #include <cstdio>
  2 #include <string>
  3 #include <memory.h>
  4 #include <algorithm>
  5 #include <stdlib.h>
  6 #include <math.h>
  7 #include <iostream>
  8 #include<queue>
  9 #include <vector>
 10 #include <bitset>
 11 using namespace std;
 12 
 13 int volume[3];
 14 int  c;
 15 bool flag = false;
 16 int visited[105][105];
 17 
 18 struct state {
 19     int pot[3];
 20     vector<int> oper;
 21     int step;
 22     state(int _x, int _y)  {
 23         step = 0;
 24         pot[1] = _x, pot[2]=_y;
 25     }
 26     void fill(int x) {
 27         pot[x] = volume[x];
 28     }
 29     void drop(int x) {
 30         pot[x] = 0;
 31     }
 32     void pour(int x, int y) {
 33         if (pot[x] <= (volume[y]-pot[y])) {
 34             pot[y] += pot[x];
 35             pot[x] = 0;
 36         }
 37         else if (pot[x] >(volume[y]-pot[y])) {
 38             pot[x] -= volume[y] - pot[y];
 39             pot[y] = volume[y];
 40         }
 41     }
 42 };
 43 queue<state> all;
 44 
 45 void bfs() {
 46     while (!all.empty()) {
 47         state now = all.front();
 48         all.pop();
 49         if (now.pot[1] == c || now.pot[2] == c) {
 50             flag = true;
 51             cout << now.step << endl;
 52             vector<int>::iterator i1 = now.oper.begin(), i2 = now.oper.end();
 53             for (; i1 != i2; i1++) {
 54                 if (*i1 == 1)
 55                     printf("FILL(1)\n");
 56                 else if (*i1 == 2)
 57                     printf("FILL(2)\n");
 58                 else if (*i1 ==3)
 59                     printf("DROP(1)\n");
 60                 else if (*i1 == 4)
 61                     printf("DROP(2)\n");
 62                 else if (*i1 == 5)
 63                     printf("POUR(1,2)\n");
 64                 else if (*i1 == 6)
 65                     printf("POUR(2,1)\n");
 66             }
 67             return;
 68         }
 69         state newone(now);
 70         if(newone.pot[1]!=volume[1])
 71             newone.fill(1);
 72         if (visited[newone.pot[1]][newone.pot[2]] == 0)
 73         {
 74             newone.step++;
 75             newone.oper.push_back(1);
 76             visited[newone.pot[1]][newone.pot[2]] = 1;
 77             all.push(newone);
 78         }
 79         newone = now;
 80         if (newone.pot[2] != volume[2])
 81             newone.fill(2);
 82         if (visited[newone.pot[1]][newone.pot[2]] == 0)
 83         {
 84             newone.step++;
 85             newone.oper.push_back(2);
 86             visited[newone.pot[1]][newone.pot[2]] = 1;
 87             all.push(newone);
 88         }
 89         newone = now;
 90         if (newone.pot[1] != 0)
 91             newone.drop(1);
 92         if (visited[newone.pot[1]][newone.pot[2]] == 0)
 93         {
 94             newone.step++;
 95             newone.oper.push_back(3);
 96             visited[newone.pot[1]][newone.pot[2]] = 1;
 97             all.push(newone);
 98         }
 99         newone = now;
100         if (newone.pot[2] != 0)
101             newone.drop(2);
102         if (visited[newone.pot[1]][newone.pot[2]] == 0)
103         {
104             newone.step++;
105             newone.oper.push_back(4);
106             visited[newone.pot[1]][newone.pot[2]] = 1;
107             all.push(newone);
108         }
109         newone = now;
110         if (newone.pot[1] != 0)
111             newone.pour(1, 2);
112         if (visited[newone.pot[1]][newone.pot[2]] == 0)
113         {
114             newone.step++;
115             newone.oper.push_back(5);
116             visited[newone.pot[1]][newone.pot[2]] = 1;
117             all.push(newone);
118         }
119         newone = now;
120         if (newone.pot[2] != 0)
121             newone.pour(2, 1);
122         if (visited[newone.pot[1]][newone.pot[2]] == 0)
123         {
124             newone.step++;
125             newone.oper.push_back(6);
126             visited[newone.pot[1]][newone.pot[2]] = 1;
127             all.push(newone);
128         }
129     }
130 }
131 
132 int main()
133 {
134     scanf("%d%d%d", &volume[1], &volume[2], &c);
135     state origin(0, 0);
136     all.push(origin);
137     visited[0][0] = 1;
138     bfs();
139     if (flag == false)
140         printf("impossible\n");
141     return 0;
142 }
View Code

没看见要输出步数……

转载于:https://www.cnblogs.com/yalphait/p/9235814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值